0

I'm having problem in getting data from firestore.I can see all the document exist in firestore collection.But it returns empty list inside stream builder. Here is my code:

// Chat Screen.Dart

   class ChatScreen extends StatefulWidget {
  static final routeName = "/chat";

  @override
  _ChatScreenState createState() => _ChatScreenState();
}

class _ChatScreenState extends State<ChatScreen> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Inbox"),),
      body: FutureBuilder<String>(
          future: DatabaseService.instance.getUserId(),
          builder: (context, idSnap) {
            if (idSnap.connectionState == ConnectionState.done) {
              print("id snap :${idSnap.data}");
              return StreamBuilder<QuerySnapshot>(
                  stream: DatabaseService.instance.chatListStream(idSnap.data),
                  builder: (context, chatsSnap) {
                    if (chatsSnap.connectionState == ConnectionState.waiting) {
                      return Text("waiting for the data");
                    } else if (chatsSnap.hasError) {
                      return Text('error ${chatsSnap.error}');
                    } else {
                      print("has data");
                      print("document found: ${chatsSnap.data.docs.length}"); // it prints 0 
                      return Text("data is loaded");
                    }
                  });
            } else {
              return Container();
            }
          }
      ),
    );
  }


 
// DatabaseService.dart

class DatabaseService{
 
  DatabaseService._privateConstructor();
  static final DatabaseService instance = DatabaseService._privateConstructor();
  FirebaseFirestore firestore = FirebaseFirestore.instance;

  Future<String>getUserId() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String userId = prefs.getString('user_id') ?? '0';
    return userId;
  }
 
  Stream<QuerySnapshot>chatListStream(String userId){
    return firestore.collection('users').doc(userId).collection('chatList').snapshots();
  }
 
}

I have checked The collection and document title which is correct and the userId being passed is not null and valid data.Here is the Screenshot of firestore database :

What am I doing wrong here?

2
  • print("id snap :${idSnap.data}"); did you get a valide id ? Commented May 24, 2021 at 9:56
  • yes it gives valid id Commented May 24, 2021 at 10:15

1 Answer 1

4

Look at how the documents in the chatList subcollection(s) are displayed in an italic font: this means that, in the console, these documents are only present as "container" (or "placeholder") of one or more sub-collection(s) but that they are not "genuine" documents, i.e. they don't exist in the Firestore database. Therefore querying documents in one of these chatList subcollections will return an empty QuerySnapshot.

You have probably created documents directly under a sub-collection of each of these chatList documents, without creating the chatList documents themselves.


So, in your case, if you need to have genuine docs in the chatList collections, you need to create them at the same time you create the first subcollection doc.

Also, note that Cloud Firestore has shallow reads: querying for documents in a collection doesn't pull in data from subcollections.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.