简体   繁体   中英

How to read read a value from firebase document field

here is the firebase firestore section

在此处输入图像描述

how do I read the value 'purchase-id'

this is how I did, but not working

var collection = FirebaseFirestore.instance.collection('users');
          final docSnapshot = await collection.doc('$index').get();
          
          Map<String, dynamic> data = docSnapshot.data()!;
          final purID = data['purchased-id'];

the value is receiving but as the future value here is how the value is receiving

final purchaseID = coursePurchaseCheck

but this is a Future value, how do I parse that to normal data

how do I properly get document value from firebase??

Check your index is equal to document key ??

You can get document key with below code:

 List<String> _userKey = [];
 await fireStore
      .collection('user').get()
      .then((QuerySnapshot querySnapshot) {
    for (var doc in querySnapshot.docs) {
      _userKey.add(doc.id);
    }
  });

And get data below:

for(String _key in _userKey){
   await FirebaseFirestore.instance
          .collection('user')
          .doc(_key)
          .get()
          .then((DocumentSnapshot documentSnapshot) async {
        if (documentSnapshot.exists) {
          var data = documentSnapshot.data();
          var res = data as Map<String, dynamic>;
          final purID = res['purchased-id'];
        }
      });
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM