Flutter Firestore检查文档是否已存在

时间:2019-04-11 10:36:25

标签: dart flutter google-cloud-firestore

Firestore在交易中调用了两次。

我创建了一个Firestore交易来检查文档是否已经存在。

var collection = _firestore.collection('players').document(_userID);

print('Start');
_firestore.runTransaction((transaction) async{
      DocumentSnapshot snapshot = await transaction.get(collection);
      if(snapshot.exists){
        print('Retrieve');
        _retrieveCloud();
      }else{
        print('Save');
        _saveToCloud();
      }
      print('End');
    }).then((v){
      print('Done');
    });

为什么叫了两次? 结果是(如果快照存在于云中):

-开始

-检索,

-结束

-完成

此后,它再次调用:

-检索,

-结束

但是没有完成。

你能告诉我为什么叫了两次吗?如何预防呢?还是有其他方法可以检查文档是否已经存在?

0 个答案:

没有答案
相关问题