简体   繁体   English

遍历 Firestore 集合中的所有文档,并将这些文档添加到 Flutter 中的另一个集合中

[英]Loop through all documents in a Firestore collection and add those documents to another collection in Flutter

I'm trying to loop through all the documents and their fields in a Firestore collection and add them to another collection with the click of a button in my flutter app.我正在尝试遍历 Firestore 集合中的所有文档及其字段,并通过单击我的 flutter 应用程序中的按钮将它们添加到另一个集合中。

So far I have this function that reads through all the documents in the collection and prints them to the console no problem:到目前为止,我有这个 function 读取集合中的所有文档并将它们打印到控制台没有问题:

documentsLoopFromFirestore() {
     FirebaseFirestore.instance
      .collection('myCollection')
      .get()
      .then((idkWhatGoesHereButICantRemoveIt) {
        idkWhatGoesHereButICantRemoveIt.docs.forEach((result) {
      print(result.data());
    });
  });
}

Using a button to call documentsLoopFromFirestore() :使用按钮调用documentsLoopFromFirestore()

_button(){
  return ElevatedButton(
    onPressed: () {
      documentsLoopFromFirestore();
    }, 
    child: Text('press me'));
}

I successfully get all the data from my Firestore documents printed in the console:我成功地从控制台中打印的 Firestore 文档中获取了所有数据:

I/flutter (23876): {lastName: smith, name: peter}
I/flutter (23876): {lastName: doe, name: john}

Now, I have tried removing the print() from print(result.data()) and then calling these 2 things on my onPressed: () {} button but they both fail:现在,我尝试从print(result.data())中删除print() ),然后在我的onPressed: () {}按钮上调用这两个东西,但它们都失败了:

- First one - 第一

onPressed: () {
  FirebaseFirestore.instance.collection('myOtherCollection222').add(documentsLoopFromFirestore());
}

- Error: - 错误:

E/flutter (23876): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: Null check operator used on a null value

- Second one - 第二个

onPressed: () {
  documentsLoopFromFirestore().forEach((doc) => {
          FirebaseFirestore.instance.collection('myOtherCollection69').add(doc)});
}

- Error: - 错误:

════════ Exception caught by gesture ═══════════════════════════════════════════
The method 'forEach' was called on null.
Receiver: null
Tried calling: forEach(Closure: (dynamic) => Set<Future<DocumentReference<Map<String, dynamic>>>>)
════════════════════════════════════════════════════════════════════════════════

I know there's some "obvious" thing I should be applying to result.data() but I'm just completely out of ideas.我知道我应该将一些“明显”的东西应用于result.data()但我完全没有想法。

Can anyone see what am I missing?谁能看到我错过了什么?

first of all idkWhatGoesHereButICantRemoveIt is the name you give the value that returns, usualy i call it value , you cant remove it because the function would not know what to operate on.首先idkWhatGoesHereButICantRemoveIt是你给返回值的名称,通常我称之为value ,你不能删除它,因为 function 不知道要操作什么。

I think you can to everything on the documentsLoopFromFirestore function, try this.我想你可以到documentsLoopFromFirestore LoopFromFirestore function 上的所有内容,试试这个。

  void documentsLoopFromFirestore() {
    FirebaseFirestore.instance.collection('myCollection').get().then(
      (value) {
        value.docs.forEach(
          (result) {
            FirebaseFirestore.instance.collection('myOtherCollection222').add(
                  result.data(),
                );
          },
        );
      },
    );
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 遍历集合中的所有firestore文档并在flutter中更新它们 - Loop through all firestore documents in a collection AND update them in flutter 这是如何遍历 flutter 中的 Firestore 集合上的文档吗? - Is this how to iterate through documents on a Firestore collection in flutter? 从 Flutter 中的 Firestore 集合中获取所有文档 - Get all documents from a Firestore collection in Flutter 如何使用 Flutter 删除 Firestore 中集合中的所有文档 - How to Delete all documents in collection in Firestore with Flutter 如何获取 Firestore 上另一个集合内的集合内的所有文档? - How to get all documents that are inside of a collection that is inside of another collection on Firestore? 如何查询集合中的多个文档(Firestore/Flutter) - How to query through multiple documents in a collection (Firestore/Flutter) 循环通过 Flutter 中集合中的 Firestore 文档时的 Null 值 - Null value when looping through Firestore documents in a collection in Flutter Firestore/Flutter:遍历文档集合的 ID 和 output 它们作为列表 - Firestore/Flutter: Iterating through the ID of a collection of documents and output them as a list 如何从保存在另一个集合中的 id 作为 flutter firestore 中的文档检索存储在一个集合中的文档 - How to retrieve documents stored in one collection from the ids saved in another collection as documents in flutter firestore Flutter:如何在 Firestore 中获取集合的所有文档名称 - Flutter: How can I get all documents name of a collection in Firestore
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM