string' is not a subtype of type 'int' of 'index' flutter while fetchi api data into listview. Flutter

Viewed 94

Here I am trying to fetcht the data from api and passing it to listview using streambuilder

class _MyHomePageState extends State<MyHomePage> {
    
      String _url = "https://owlbot.info/api/v4/dictionary";
      String _token = "f2b517e6582daa3acc7df5f259c5707dc9d2147e";
    
      TextEditingController _controller = TextEditingController();
    
      StreamController _streamController;
      Stream _stream;
    
      _search() async{
        if(_controller.text == null || _controller.text.length == 0){
          _streamController.add(null);
        }
        Response response = await get(_url + _controller.text.trim(), headers: {"Authorization": "Token " + _token});
        if(response.statusCode == 200){
        var encode = json.encode(response.body);
        
        _streamController.add(json.decode(encode));
        }
        else{
          print(response.statusCode);
        }
      }

This is the code where i want to show my data in list view but I am getting an error (string' is not a subtype of type 'int' of 'index' )

 return ListView.builder(
                itemCount: snapshot.data["definitions"].length ,
                itemBuilder: (BuildContext context, int index){
                  return ListBody(
                    children: <Widget>[
                      Container(
                        color: Colors.grey[300],
                        child: ListTile(
                          leading: snapshot.data["definitions"][index]["image_url"] = null ? null : CircleAvatar(
                            backgroundImage: NetworkImage(snapshot.data["definitions"][index]["image_url"]),
                          ),
                        ),
                      ),
                    ],
                  );
                },
              );
1 Answers

Check this

return ListView.builder(
                itemCount: snapshot.data["definitions"].length ,
                itemBuilder: (BuildContext context, int index){
                  return ListBody(
                    children: <Widget>[
                      Container(
                        color: Colors.grey[300],
                        child: ListTile(
                          leading: snapshot.data["definitions"][index]["image_url"] = null ? null : CircleAvatar(
                            backgroundImage: NetworkImage((snapshot.data["definitions"] as List)[index]["image_url"]),
                          ),
                        ),
                      ),
                    ],
                  );
                },
              );
Related