Home > front end >  Exception: type 'String' is not a subtype of type 'Map<dynamic, dynamic>'
Exception: type 'String' is not a subtype of type 'Map<dynamic, dynamic>'

Time:08-12

When i creat a realtime database table then i get the table datas but i got an error: type 'String' is not a subtype of type 'Map<dynamic, dynamic>' in type cast

FirebaseAnimatedList(
        query: dbRef,
        itemBuilder: (BuildContext context, DataSnapshot snapshot,
          Animation<double> animation, int index) {
        Map data = snapshot.value as Map;
        data['key'] = snapshot.key;
        return listItem(data);
         },
      ),

CodePudding user response:

Here snapshot.data is a map and snapshot.value is string

Try

Map data = snapshot.data as Map;

CodePudding user response:

You need to convert the dynamic value which comes in string into decoded jsonMap if it is a map using jsonDecode. Or, you can use snapshot.data which comes in terms of key, value pair Map.

FirebaseAnimatedList(
        query: dbRef,
        itemBuilder: (BuildContext context, DataSnapshot snapshot,
          Animation<double> animation, int index) {
        Map data = jsonDecode(snapshot.value.toString());

// or
Map data = snapshot.data;


// Now do what you want to do
        data['key'] = snapshot.key;
        return listItem(data);
         },
      ),
  • Related