0
votes

In my App, I need to get the Profile-Data of the current user before continuing. I try to accomplish this by using a StreamBuilder in my Code.

My Problem: The StreamBuilder always returns the else statement. As if he cannot load any data from the FireStore-Collection 'Users'.

return StreamBuilder<QuerySnapshot>(
        stream: Firestore.instance.collection('Users').snapshots(), // It seems like he cannot load any data from the collection 'Users'...
        builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot){
          if (snapshot.hasData) {
            print("Loaded data successfully");
            return Container(color: CupertinoColors.systemGreen,); 
          } else {
            print("Could not load data");
            return Container(color: CupertinoColors.systemRed,); // The Code always ends here
          }
        }
      );

Furthermore I don't get any kind of Error-Message.

Flutter Doctor:

[✓] Flutter (Channel stable, 1.20.4, on Mac OS X 10.15.6 19G2021, locale de-DE)
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
[✓] Xcode - develop for iOS and macOS (Xcode 12.1)
[✓] Android Studio (version 4.0)
[✓] Connected device (1 available)

• No issues found!

Why don't I get any data from Firestore?

EDIT:

My Firestore "Users" Collection (Sammlung):

Image of my Firestore collection

2

2 Answers

0
votes

Try this,

return StreamBuilder(
                stream: Firestore.instance
                    .collection('Users')
                    .snapshots(),
                builder: (BuildContext context,
                    AsyncSnapshot<QuerySnapshot> snapshot) {
                  print("snapshotData"+snapshot.data.toString());
                  if (snapshot.data.toString()=='null')
                    return Container(color: CupertinoColors.systemRed); 
                  if (snapshot.hasError)
                     return Container(color: CupertinoColors.systemRed); 
                  if(snapshot.data.documents.length<1)
                   return Container(color: CupertinoColors.systemRed); 
                  switch (snapshot.connectionState) {
                    case ConnectionState.waiting:
                      return new Text('Loading...');
                    default:
                     return Container(color: CupertinoColors.systemGreen); 
                  }
                },
              );

i am adding condition for blank/null as well.

0
votes

The StreamBuilder looks good, check if the "Users" collection is created and has data there, it seems to me that this is what is failing. The problem is not in Flutter, it is more a problem with your data, that is why you do not get an output error.