0
votes

I have an error when trying to parse my JSON and display it in the text, not as a listview.

Below is my JSON structure.

{
"status": "success",
    "data": {
            "id": 5,
            "name": afri,
            "class": 3c'
            },
    "message": "Success"
}

and this is my Future API async.

Future<Map> getData() async {
var response = await http.get(
    'url');
return json.decode(response.body) as Map<String, dynamic>;
}

and this is my code to display the JSON.

Container(
          child: FutureBuilder<Map>(
            future: getData(),
            builder: (context, snapshot) {
              Map<String, dynamic> data = jsonDecode(snapshot.data["data"]);
              return Card(
                child: Container(
                  padding: EdgeInsets.all(8),
                  color: Colors.red,
                  child: Text(
                    data['port_website'],
                    style: TextStyle(color: Colors.white, fontSize: 24),
                  ),
                ),
              );
            },
          ),
        ),

and when I'm trying to get the data, I got an error like this.

type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String'

which code should I fix?

1
i've read that questio before. but still, i dont get it. im sorry, its hard for me because im beginner. :( - Aphron
I think you need to change this Map<String, dynamic> data = jsonDecode(snapshot.data["data"]); with this List<dynamic> data = jsonDecode(snapshot.data["data"]); - E. Shcherbo
and i got error like this - Aphron
════════ Exception caught by widgets library ═══════════════════════════════════ The method '[]' was called on null. Receiver: null Tried calling: []("data") The relevant error-causing widget was FutureBuilder<Map<dynamic, dynamic>> lib\…\Destination\destinationDetail.dart:304 ═══════════════════════════════════════════════════════════════════ ════════ Exception caught by widgets library ═══════════════════════════════════ type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String' The relevant error-causing widget was FutureBuilder<Map<dynamic, dynamic>> - Aphron

1 Answers

1
votes

You can try to convert the runtime of data from _InternalLinkedHashMap to an exact list.

You can use List.form to achieve it.

final _data = List<dynamic>.from(
  data.map<dynamic>(
    (dynamic item) => item,
  ),
);

Edit 1

Worked for me. Try this

new Map<String, dynamic>.from(snapshot.value);