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

Viewed 275

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.

** UPDATE ** when tried with doc.data['name'] as per the comments, it shows following error:

enter image description here

2 Answers

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

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

UPDATE : Try doc.get('name')

Related