如何使用Streambuilder从一个文档中复制数据并将其粘贴到Firebase中的另一个文档中?

时间:2020-04-08 20:02:03

标签: firebase flutter google-cloud-firestore

我的使用Firebase / firestore的flutter应用程序出现问题。以下代码试图从一个集合中的文档中复制数据,并从该文档中获取该数据,然后将其复制到另一个集合中的另一文档中。但是,当我使用一些断点运行代码时,我注意到无论何时调用该函数,它都会跳过Streambuilder行。如果您能帮助我,将不胜感激。我已将错误放在一个函数中。

代码:

copyDocument(String copyId) async {
    bool beingCalled;
    var createdAt;
    String chattingWith, id2, nickname, photoUrl;
    DocumentReference collectionRef = Firestore().collection('messages').document(id).collection('pastchats').document(copyId);
    return StreamBuilder<DocumentSnapshot> (
      stream: Firestore.instance.collection('messages').document(copyId).snapshots(),
      builder: (context, snapshot) {
        if (!snapshot.hasData) {
          print('waiting...');  
          return CircularProgressIndicator(
            strokeWidth: 2,
          );
        } else {
          beingCalled = snapshot.data['beingCalled'];
          chattingWith = snapshot.data['chattingWith'];
          createdAt = snapshot.data['createdAt'];
          id2 = snapshot.data['id'];
          nickname = snapshot.data['nickname'];
          photoUrl = snapshot.data['photoUrl'];
          Firestore.instance.runTransaction((transaction) async {
            await transaction.set(
              collectionRef,
              {
                'beingCalled': beingCalled,
                'chattingWith': chattingWith,
                'createdAt': createdAt,
                "id" : id2,
                'nickname': nickname,
                'photoUrl': photoUrl,  
              },
            );
          }
        );       
        }
      },
    );
    }

0 个答案:

没有答案