【问题标题】:Unable to retrieve document from Firestore with Flutter无法使用 Flutter 从 Firestore 检索文档
【发布时间】:2020-10-30 08:55:16
【问题描述】:

.. 我正在将 Cloud Firestore 用于我正在进行的 Flutter 项目。

Database Screenshot

上面添加的是我的数据库模型的截图。

基本上每个用户文档都有自己的个人信息字段和他/她的项目的集合。

Project Example 1

Project Example 2

以上是两个扩展项目的屏幕截图。它包含项目的详细信息,如 CR、字段、成员等。

 x = StreamBuilder(
          stream: _projectStreamLock ? null : Firestore.instance.collection('User').document(_savedUser.user.uid).collection('Projects').snapshots(),
          builder: (BuildContext context,AsyncSnapshot<QuerySnapshot> snapshots){
            if(snapshots.hasData && !_projectStreamLock){

              List<DocumentSnapshot> projectList = snapshots.data.documents;

              _savedUser.user.projects = [];

              for(int i=0;i<projectList.length;i++){
                _savedUser.user.projects.add(Project(name: projectList[i].documentID,field: projectList[i].data["Field"],subField: projectList[i].data["SubField"],completionRate: projectList[i].data["CR"],members: projectList[i].data["Members"]));
              }

              return ListView.builder(
                  itemCount: snapshots.data.documents.length,
                  itemBuilder: (_,int index){
                    print(snapshots.data.documents[index].documentID + " : " + snapshots.data.documents[index].data["CR"].toString());
                    return ProjectCard(
                      globalHeight: _height,globalWidth: _width,
                      projectDetails: Project(
                          name : snapshots.data.documents[index].documentID,
                          completionRate: snapshots.data.documents[index].data["CR"],
                          field: snapshots.data.documents[index].data["Field"],
                          members: snapshots.data.documents[index].data["Members"],
                          subField: "Hello",
                      ),
                    );
                  });
              ///
            }
            else{
              _projectStreamLock = false;
              return ListView.builder(
                //  key: new Key(Random.secure().nextDouble().toString()),
                  itemCount: _savedUser.user.projects.length,
                  itemBuilder: (_,int index){
                    return ProjectCard(globalHeight: _height,globalWidth: _width,projectDetails: _savedUser.user.projects[index],);
                  });
            }
          },
        );

上面是创建代表每个项目的卡片列表的代码。代码可能看起来有点乱,但请关注第一个“if”案例中的 ListView.builder Widget。在下面的代码 sn-p 中,我从快照中提取数据并创建每个项目的项目对象。

我面临的问题是,在获取第一个项目(E-Cell App)的 CR 值时。我得到一个空值。但我得到了所有其他项目的正确 CR 值。正如您在代码 sn-p 中看到的那样,我正在打印列表中添加的每个项目的名称 (documentID) 和 CR。 下面是输出。

Print statement Output

如仅第一个项目(E-Cell App)的输出 CR 值为空。不包括我得到了所有其他项目的正确价值。我不确定是什么导致了这个问题。在所有项目中,CR 采用相同的格式(数字)。正如上面发布的图片“项目示例 1”是给出空 CR 值的图片,“项目示例 2”提供了正确的 CR 值,就像所有其他项目一样。提前感谢您的帮助。

【问题讨论】:

    标签: flutter dart google-cloud-firestore


    【解决方案1】:

    你犯了我几天前犯的同样的错误。

    您在“E-Cell”文档的字段名称中添加了一个空格

    对于 Firestore,“Cr” 和 “Cr” 是不同的字段。

    注意第一个空间很小,很难指出。

    另外,我加入 stack-overflow 已经有几天了,您在问题中包含的信息对我找出错误有很大帮助。您的问题无疑是其他人的榜样。

    【讨论】:

    • 非常感谢您的回答........这是一个我永远不会发现的愚蠢错误。
    • 哈哈,当我注意到它时,我很高兴。从我的错误中吸取教训来帮助别人,感觉很棒。
    • 嘿兄弟....我这里还有一个问题:stackoverflow.com/questions/62834339/… ....你能回答一下吗......这将是一个很大的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-02
    • 2019-04-26
    • 2021-04-13
    • 1970-01-01
    • 2019-12-15
    • 2020-05-16
    • 1970-01-01
    相关资源
    最近更新 更多