Home > Enterprise >  How to get value with key from a Map<String, dyanmic> in Flutter
How to get value with key from a Map<String, dyanmic> in Flutter

Time:10-10

I am new to flutter and Firestore, and still leaning.

I have been trying to print a single value from a Map<String, Dynamic>. When I print the whole map it works but when tried to print a single value with key, it produces a null error.

getUsersFromFireStore(String appUid) async {
    List<Map<String, dynamic>> list = [];
    await instance.collection('Users').where('refDocument', isEqualTo: appUid).get()
        .then((querySnapshotOne) {
      return querySnapshotOne.docs[0].reference.collection('Data').orderBy('timeStamp', descending: true).get();
    }).then((querySnapshotTwo) {
      if(querySnapshotTwo.size > 0){
        querySnapshotTwo.docs.forEach((doc) {
          String id = doc.id;
          list.add(doc.data()); // Here is the problem
          // print(doc.data().toString()); produces {age:24, name: Hari Ram}
          // But doc.data()['name'] doesn't work, gives null error
        });
      }
    });
    return list;
}

Please help. Thanks in advance.

CodePudding user response:

Remove () after the data keyword. i.e. print(doc.data['name'])

CodePudding user response:

doc.data['name'] // This should solve your problem

  • Related