简体   繁体   English

循环通过 Flutter 中集合中的 Firestore 文档时的 Null 值

[英]Null value when looping through Firestore documents in a collection in Flutter

I have the following function to loop through the documents and their fields that I have in my Firestore collection and display them as a map in the console:我有以下 function 来循环浏览我在 Firestore 集合中拥有的文档及其字段,并在控制台中将它们显示为 map:

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

When I call documentsLoopFromFirestore() in a button with:当我在按钮中调用documentsLoopFromFirestore()时:

ElevatedButton(
 onPressed: () {
   print(documentsLoopFromFirestore());
 }, 

I get the following result in the console:我在控制台中得到以下结果:

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

It successfully prints the values of my documents in that Firestore collection, but just before it does so, it throws that null that is not allowing my to add this map to another collection which is my objective.它成功地在该 Firestore 集合中打印了我的文档的值,但在此之前,它会抛出不允许我将此null添加到另一个集合中的 null,这是我的目标。

If I add async and await to the function:如果我将asyncawait添加到 function:

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

Then I get:然后我得到:

I/flutter (29803): Instance of 'Future<dynamic>'
I/flutter (29803): {lastName: smith, name: peter}
I/flutter (29803): {lastName: doe, name: john}

It seems like it throws the null when doing the first loop.似乎在执行第一个循环时会抛出null Does anyone knows how I can get rid of this null ?有谁知道我怎样才能摆脱这个null

Change this print(documentsLoopFromFirestore());更改此打印(documentsLoopFromFirestore()); to documentsLoopFromFirestore();到文档LoopFromFirestore(); in ElevatedButton在 ElevatedButton 中

The reason you see a null with the first code and the Instance od Future<dynamic> is that you first print out your function and the the result.您看到带有第一个代码和Instance od Future<dynamic>null的原因是您首先打印出 function 和结果。

In the first case the function soend't return anything so you see null and in the second the function is a Furture to you just print that it is a future.在第一种情况下,function 不会返回任何内容,因此您会看到null ,在第二种情况下,function 是未来,您只需打印它就是未来。

To awoid that either remove the first print or return the values in the format you want in the function.为了避免删除第一个打印或以 function 中所需格式返回值。

暂无
暂无

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

相关问题 这是如何遍历 flutter 中的 Firestore 集合上的文档吗? - Is this how to iterate through documents on a Firestore collection in flutter? Firestore 和 flutter:当字段为 null 时更新文档 - Firestore and flutter: update documents when a field is null 如何查询集合中的多个文档(Firestore/Flutter) - How to query through multiple documents in a collection (Firestore/Flutter) 遍历集合中的所有firestore文档并在flutter中更新它们 - Loop through all firestore documents in a collection AND update them in flutter Firestore/Flutter:遍历文档集合的 ID 和 output 它们作为列表 - Firestore/Flutter: Iterating through the ID of a collection of documents and output them as a list 遍历 Firestore 集合中的所有文档,并将这些文档添加到 Flutter 中的另一个集合中 - Loop through all documents in a Firestore collection and add those documents to another collection in Flutter Flutter Firestore:集合中的特定文档(Firestore.collection.where) - Flutter Firestore: specific documents from a collection (Firestore.collection.where) 从 Flutter 中的 Firestore 集合中获取所有文档 - Get all documents from a Firestore collection in Flutter 如何使用 Flutter 删除 Firestore 中集合中的所有文档 - How to Delete all documents in collection in Firestore with Flutter 在 Flutter 中的嵌套集合中查询 Firestore 文档 - Query Firestore documents inside a nested collection in Flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM