Home > Enterprise >  initially empty list but dynamically adding item to list is giving error in flutter
initially empty list but dynamically adding item to list is giving error in flutter

Time:09-05

I have a empty list and I add item to it dynamically.I tried to access list item but got an error.It gives an error of Exception has occurred. NoSuchMethodError (NoSuchMethodError: Class '_InternalLinkedHashMap<String, Object>' has no instance getter 'id'. Receiver: _LinkedHashMap len:3 Tried calling: id)

List likesCount=[];

I add item to list

           for(var j=0;j<snapshot.data!.length;j  ){
                
                  likesCount.add({"like":snapshot.data![j].likes!.length,"id":snapshot.data![j].postId,"index":j});
            
          }

I tried to access with

                           if(likesCount.isNotEmpty){
                              for(var m=0;m<likesCount.length;m  ){
                                Text(likesCount[m].id)
                              }
                             }

CodePudding user response:

you have List<Map> thats why you need to access the data with map too.

the issue is here Text(likesCount[m].id . you call the id with assumtion its an List<Object>

solution:

 if(likesCount.isNotEmpty){
    for(var m=0;m<likesCount.length;m  ){
      Text(likesCount[m]["id"]) // this is how to get value in map
    }
 }

  • Related