1

I'm writing a flutter method in which I want to parse a simple JSON data

{"alm":3,"co2":1,"hu":2,"temp":32,"th":11,"tm":31,"ts":41}

I tried to parse it in a simple get class

List data;
  Future<String> getData() async {
    http.Response response = await http.get(
        Uri.encodeFull("http://chicken20.pythonanywhere.com/jsonn"),
        headers: {"Accept": "application/json"});
    print(response.body);
    data = json.decode(response.body);
    print(data);
    return "Success!";

and this how I tried to use in material app

body: new ListView.builder(
          itemCount: data == null ? 0 : data.length,
          itemBuilder: (BuildContext context, int index) {
            return new ListTile(
              leading: const Icon(Icons.stay_primary_portrait),
              title: Text(data.length.toString()),
              subtitle:
              Text('${_deviceData}'),
              trailing: Icon(Icons.more_vert),
              dense: true,
              isThreeLine: true,
            );
          },
        ),

and I got this error

[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<dynamic>'

and nothing was shown in the screen

5
  • The data from your API is a Map and you're trying to assign it to a List and use in a ListView. What are you trying to accomplish? Commented Jun 23, 2020 at 12:06
  • if you try to enter to the link you will find a small json data i want to show it in a list view Commented Jun 23, 2020 at 12:08
  • The data seem to be a Map. you cant display it in a ListView Commented Jun 23, 2020 at 12:09
  • Maybe you want to display it in a ListTile and not a ListView Commented Jun 23, 2020 at 12:11
  • yes yes that's it Commented Jun 23, 2020 at 12:12

3 Answers 3

1

First thing the response from API is not a json list, its a simple json object. If you wanna iterate over the keys of the above object you can create a list with all the keys and then use key index to get the actual key name in the response.

final List<String> keys = ["alm","co2","hu","temp","th","tm","ts"];

then you can modify your body widget like following -

body: new ListView.builder(
        itemCount: data == null ? 0 : data.length,
        itemBuilder: (BuildContext context, int index) {
          return new ListTile(
            leading: const Icon(Icons.stay_primary_portrait),
            title: Text(data.length.toString()),
            subtitle: Text(data[keys[index]]),
            trailing: Icon(Icons.more_vert),
            dense: true,
            isThreeLine: true,
          );
        },
      ),

Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

0

Your Url hai double 'n' in json. are you sure about that?

1 Comment

yes when i print response.body it show me the response in console
0

You're getting a Map from your API. you cannot use that on a ListView.builder. You can use a regular ListView and add the ListTiles manually.

Create the ListView and add the ListTiles manually using each key in the map.

body: ListView(
        children: <Widget>[
          buildTile('alm'),
          buildTile('co2'),
          buildTile('hu'),
          buildTile('temp'), // continue this for others
        ],
      ),

Create the ListTile to be used in the ListView

buildTile(String key){
    return ListTile(
      leading: const Icon(Icons.stay_primary_portrait),
      title: Text('$key'),
      subtitle: Text('${data['$key']}'),
      trailing: Icon(Icons.more_vert),
      dense: true,
      isThreeLine: true,
    );
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.