Flutter firestore access value from .get().then function outside of the .then

Viewed 29

I have this set of code:

      String weightValue = "start";
      //todo: get current weight format from user settings
      db.collection('UserSettings').doc(userID).get().then((value) {
        weightValue = value['weightFormat'];
      });
      print(weightValue);

I can print the weightValue insite the .then() but I can not access the weightValue outside of the .then()

I have tried to not use a .then() and use the await but I am in a streamBUilder:

  builder:
      (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
    if (snapshot.hasData) {
      String weightValue = await getWeightSettings(userID, db);

and if I await that fucntion I created to get the value, I have to make the builder async:

 (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) async {

and then I get this error:

The argument type 'Future Function(BuildContext, AsyncSnapshot<QuerySnapshot<Object?>>)' can't be assigned to the parameter type 'Widget Function(BuildContext, AsyncSnapshot<QuerySnapshot<Map<String, dynamic>>>)'

0 Answers
Related