【问题标题】:How to "await" non-future variable?如何“等待”非未来变量?
【发布时间】:2019-08-19 05:46:10
【问题描述】:

我的州有DocumentReference locationDocumentRef;

我正在根据引用更改locationDocumentRef,无论是通过查询还是通过添加新文档来收集。

所以我有这个功能来检查文件,如果有一个设置其引用locationDocumentRef,或者添加一个新的并将其引用设置为locationDocumentRef。我每次都通过将其设置为 null 来重置它的值,因为我不想获得以前的结果。但它打印空。

所以我的问题是,我怎样才能解决它们并获得价值?我认为我在代码中解决得太早了,所以我不能等待一个非未来的值。我该如何解决?

void firestoreCheckAndPush() async {
  setState(() {
    locationDocumentRef = null;
  });
  bool nameExists = await doesNameAlreadyExist(placeDetail.name);
  if (nameExists) {
    print('name exist');
  } else {
    print('name will be pushed on firestore');
    pushNameToFirestore(placeDetail);
  }
  var resolvedRef = await locationDocumentRef;
  print(resolvedRef.documentID); // I get null here
}

这些是我用过的功能

Future<bool> doesNameAlreadyExist(String name) async {
      QuerySnapshot queryDb = await Firestore.instance
          .collection('locations')
          .where("city", isEqualTo: '${name}')
          .limit(1)
          .getDocuments();

      if (queryDb.documents.length == 1) {
        setState(() {
          locationDocumentRef = queryDb.documents[0].reference;
        });
        return true;
      } else {
        return false;
      }
    }

还有一个

void pushNameToFirestore(PlaceDetails pd) async {
      DocumentReference justAddedRef =
          await Firestore.instance.collection('locations').add(<String, String>{
        'city': '${pd.name}',
        'image': '${buildPhotoURL(pd.photos[0].photoReference)}',
      });
      setState(() {
        locationDocumentRef = justAddedRef;
      });
    }

【问题讨论】:

    标签: dart flutter async-await google-cloud-firestore


    【解决方案1】:

    我在这里首先看到了两个错误 var resolvedRef = 等待 locationDocumentRef; 为什么要等待 locationDocumentRef, 其次,您无需等待 pushNameToFirestore(PlaceDetails pd) firestoreCheckAndPush() 函数,这很奇怪,因为 pushNameToFirestore(String) 是同步的,这意味着您不会等待它完成,因此如果您要添加新名称,它将打印 null。 如果我错了,请纠正我。 你可以在这里找到更多关于同步和未来的信息https://www.dartlang.org/tutorials/language/futures look at the graph at the middle of the page

    【讨论】:

    • 等待 pushNameToFirestore(PlaceDetails pd) 完成解决了我的问题,谢谢!
    【解决方案2】:

    试试这个

    Future<List<DocumentSnapshot>> doesNameAlreadyExist(String name) async {
        QuerySnapshot data =  await Firestore.instance
              .collection('locations')
              .where("city", isEqualTo: name)
              .limit(1)
              .getDocuments();
              return data.documents;
      }
    
    void firestoreCheckAndPush() async {
    
      var data = await doesNameAlreadyExist('yourname');
      if (data.length > 0) {
        print('name exist');;
        print('Document id '+ data[0].documentID);
      } else {
        print('name will be pushed on firestore');
      }
    }
    

    【讨论】:

    • 如何从 pushNameToFirestore 获取 documentID?在这两种情况下我都需要保存引用,如果数据存在,获取存在的引用或数据不存在,添加新的并返回刚刚添加的数据引用。
    【解决方案3】:

    看看下面的代码。

    void firestoreCheckAndPush() async {
        DocumentReference documentReference;
        var data = await doesNameAlreadyExist('yourname');
        var dataRef = await doesNameAlreadyExist('yourname');
        if (data.length > 0) {
          print('name exist');
          documentReference = dataRef[0].reference;
          print('Document id ' + data[0].documentID);
          documentReference = dataRef[0].reference;
          print('Document reference ');
          print(documentReference);
        } else {
          print('name will be pushed on firestore');
        }
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-11
      • 1970-01-01
      • 2021-03-17
      • 2022-11-17
      • 2019-04-20
      • 2011-11-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多