skip to Main Content

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);
         },
      ),

I got Result

enter image description here

3

Answers


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

    Try

    Map data = snapshot.data as Map;
    
    Login or Signup to reply.
  2. 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);
             },
          ),
    
    Login or Signup to reply.
  3. I think your query returning single value,
    First test with it

    itemBuilder: (BuildContext context, DataSnapshot snapshot,
        Animation<double> animation, int index) {
      return Text("${snapshot.value}");
    },
    

    Now for getting map, it is possible to get null value, for this case do

    itemBuilder: (BuildContext context, DataSnapshot snapshot,
        Animation<double> animation, int index) {
      Map? data = snapshot.value as Map?;
      final String? key = snapshot.key;
      return Text("Your ItemBUilder : $key ${data.toString()}");
    },
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search