Unable to retrieve document from Firestore with Flutter

Viewed 377

.. I'm using Cloud Firestore for my ongoing Flutter Project.

Database Screenshot

Added above is a screenshot of my database model.

Basically each user document has its own personal information fields and a Collection of his/her projects.

Project Example 1

Project Example 2

Above are the screenshots of two of the projects expanded. It has the details of the project like its CR,Field,members etc.

 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],);
                  });
            }
          },
        );

Above is the code which creates a list of cards representing each project. The code might look a bit messy but please focus on the ListView.builder Widget in the first 'if' case. In the following code snippet I am extracting the data from the snapshot and creating Project Object of each project.

The issue I'm facing is that while taking the CR value of the first project (E-Cell App). I'm getting a null value. But I'm getting proper CR values of all the other projects. As you are able to see in the code snippet that I'm printing the name (documentID) and the CR of each Project I'm adding in the list. Below is the output.

Print statement Output

As seen in the output CR value of only the first project (E-Cell App) is null. Excluding that I'm getting proper values of all the rest of the projects. I'm not sure what is causing that issue. In all the projects CR is taken in same format (number). As the pictures posted above "Project Example 1" is the one giving null CR value and "Project Example 2" is providing proper CR values just like all the other projects. Thanks in advance for the help.

1 Answers

You made the same mistake i made a few days ago.

You added a space in the name of the field of your "E-Cell" document

For Firestore "Cr " and "Cr" are different fields.

notice how the first one has a small space which is hard to point out.

Also, it's been a few days since I've joined stack-overflow and the information you have included in your question helped me a lot in finding out the error. Your question is certainly an example for others.

Related