简体   繁体   English

如何从 firebase 获取数据并将其存储到列表中以供在 dart 中进一步操作

[英]How to get data from the firebase and store it into a list for futher manipulation in dart

List<DocumentSnapshot> docs = snapshot.data.docs;
_selectedSubjects = docs
    .map(
      (doc) => Subject(
        doc['SubjectName'].data(),
        doc['courseIcon'].data(),
        doc['courseCode'].data(),
      ),
    )
    .toList();

I have a class called subject that has attributes/members courseCode, courseIcon, and lastly subjectName.我有一个名为 subject 的 class,它具有属性/成员 courseCode、courseIcon,最后是 subjectName。 Everything is okay so far, but vs code is saying:到目前为止一切都很好,但是 vs code 说:

Type: dynamic类型:动态

The property 'docs' can't be unconditionally accessed because the receiver can be 'null'.不能无条件地访问属性“docs”,因为接收者可以是“null”。 Try making the access conditional (using '?.') or adding a null check to the target ('.')."尝试使访问有条件(使用 '?.')或向目标('.')添加 null 检查。”

I have already imported all the necessary libraries, as well as vs code's reccomendations.我已经导入了所有必要的库,以及 vs 代码的推荐。 What should I try?我应该尝试什么?

The error means that snapshot.data may be null , so you need to first verify that it's not null before you can assign it to your non-nullable docs variable:该错误意味着snapshot.data可能null ,因此您需要先验证它不是 null ,然后才能将其分配给不可为 null 的docs变量:

var querySnapshot = snapshot.data;
if (querySnapshot != null) {
    List<DocumentSnapshot> docs = querySnapshot.docs;
    _selectedSubjects = docs
        .map(
          (doc) => Subject(
            doc['SubjectName'],
            doc['courseIcon'],
            doc['courseCode'],
          ),
        )
        .toList();
}

The local querySnapshot variable is guaranteed to be non-null inside the if block, so now you can simply assign querySnapshot.docs to docs .本地querySnapshot变量在if块内保证为非空,因此现在您可以简单地将querySnapshot.docs分配给docs

I also removed the .data() calls, as @Gwhyyy pointed out in their comment.正如@Gwhyyy 在他们的评论中指出的那样,我还删除了.data()调用。

You might want to take a moment to read up on nullability in Dart as you're likely to encounter many more situations where the type changes based on handling null.您可能想花点时间阅读Dart中的可空性,因为您可能会遇到更多类型根据处理 null 发生变化的情况。

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

相关问题 如何使用 Flutter/Dart 从 Firebase 实时数据库中读取数据 - How to read data from Firebase Realtime database using Flutter/Dart 读取 firebase 文档并将数据复制到 dart 列表中 - Read firebase doc and copy the data into dart list 如何从 Firebase 实时数据库中获取数据到 Flutter 的列表中? - How to get data from Firebase Realtime Database into list in Flutter? 我想检索存储在作为数组存储的 firebase 中的数据。 然后我需要从 firebase 获取数组并将其存储在 flutter 列表中 - I want to retrieve data stored in firebase which is stored as an array. then I need to get the array from firebase and store it in a list in flutter 如何从firebase获取用户列表 - how to get a list of users from firebase firebase中如何正确存储数据 - How to properly store data in firebase 如何通过 flutter 中的文档从 firebase 存储中检索数据? - How do i retrieve data from firebase store by document in flutter? 如何从 firebase firestore 获取数据并将数据存储在 useState 钩子中 - How to fetch data from firebase firestore and store the data in useState hook 如何从 firebase 获取最新的更新数据? - How to get the latest updated data from firebase? 如何获取上传的图片并将其存储在firebase存储中? - How to get an image uploaded and store it in the firebase storage?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM