简体   繁体   中英

How to store field value from firestore in Flutter?

For example, in the image below, I would like to store the value "hi" in a string in Flutter from Firestore. However, when I print the value, I keep getting Instance of 'Future<dynamic>'

Any idea how to do this?

Edit: One thing I am trying to do is basically get the data and see if its equal to a specific value. For example, if the field text is equal to "hello", then I would print "hi" to the screen 在此处输入图像描述

Code:

final firestoreInstance = FirebaseFirestore.instance;
final FirebaseAuth auth = FirebaseAuth.instance;

Future<String> getString(docID) async {
  String? roleValue = '';
  DocumentSnapshot docSnapshot = await firestoreInstance
      .collection('messages')
      .doc(docID)
      .get();

  roleValue = docSnapshot.data()!['text'];
  return roleValue;
}

Seems like you are not awaiting the call as this is an async function. You have to add await just before calling the getString function.

final result = await getString('abc);
print(result);

Now the result will no more Instance of 'Future<dynamic>'

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