Flutter Complex Parsing Json Undefined getter "List<Type>'

Viewed 90

I am trying to parse a complex json file into my application I am getting an error: The getter 'name' isn't defined for the type 'List'. I can't get the name of the route at my List of routes, but can get everything else. I don't understand where this is happening and how to fix it.

My code:

void openBottomSheet() {
showModalBottomSheet(
    context: context,
    builder: (context) {
      return FutureBuilder<DriverDataModel>(
        future: mongoApi.getMongoData(),
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            final driver = snapshot.data;
            return Container(
              child: ListView.builder(
                itemCount: driver.data.routes.length,
                itemBuilder: (BuildContext context, snapshot) {
                  return ListTile(
                    title: Text('${driver.data.routes.name}'),
                    leading: Icon(Icons.directions),
                    onTap: () {
                      drawPolyLine.cleanPolyline();
                      getCurrentLocation();
                      routesCoordinates.isInCourse(driver.data.routes);
                      Navigator.pop(context);
                    },
                  );
                },
              ),                
            );
          }
          return Container();
        },
      );
    });

Json reponse:

{

"success": true,
"data": {
    "_id": "600773ac1bde5d10e89511d1",
    "name": "Joselito",
    "truck": "5f640232ab8f032d18ce0137",
    "phone": "*************",
    "routes": [
        {
            "name": "Tere city",
            "week": [
                {
                    "short_name": "mon"
                }
            ],
            "coordinates": [
                {
                    "lat": -22.446938,
                    "lng": -42.982084
                },
                {
                    "lat": -22.434384,
                    "lng": -42.978511
                }
            ]
        }
    ],
    "createdAt": "2021-01-20T00:05:00.717Z",
    "updatedAt": "2021-01-20T00:05:00.717Z",
    "__v": 0
}

I used https://app.quicktype.io/ to create my model and successfully parsed. How ever, when I tried to print the name of my route inside of my routes's list a get that getter error.

2 Answers

@fartem almost answered right except you need to dynamically access your items by index (not just the first item). In your code on the line where you use the function itemBuilder in ListView.builder, instead of

itemBuilder: (BuildContext context, snapshot) {

I would suggest using

itemBuilder: (BuildContext context, i) {

since the second parameter is an INDEX. Thus, to be able to get the name for each of the items in the list you would have to use that index:

title: Text('${driver.data.routes[i].name}'),

and so on.

Routes is an array, you can try to calling it by driver.data.routes[0].name

Related