简体   繁体   中英

StreamBuilder snapshot has no data Flutter

I took a look at some similar questions but I didn't find what I'm looking for. I have a StreamBuilder whose snapshot has no data but if I look inside the collection that it's listening I can see that there are 2 documents.

This is my first time using StreamBuilder so maybe I'm missing some basics rules. I say that it has no data because it's always displayed "No chats here" .

This is the code (stream function later):

StreamBuilder<ChatCard>(
        stream: widget.database.streamChats(widget.user),
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            return Container(
              child: Text(
                "There's something here!",
                style: TextStyle(
                  color: Colors.white,
                ),
              ),
            );
          } else {
            return Container(
              child: Text(
                "No chats here",
                style: TextStyle(
                  color: Colors.white,
                ),
              ),
            );
          }
        },
      ),

This is the stream function, there are some extra checks because I'm trying to figure it out what isn't working, by the way seems no error in try block and no if statements violated:

Stream<ChatCard> streamChats(MyUser sender) {
    if (sender != null) {
      if (sender.id != null) {
        try {
          FirebaseFirestore.instance
              .collection("ids/tabs/${sender[0].toLowerCase()}/${sender.toLowerCase()}/chats")
              .snapshots();
          print(
              "ids/tabs/${sender[0].toLowerCase()}/${sender.toLowerCase()}/chats'");
        } on FirebaseException catch (e) {
          print(e);
        } catch (e) {
          print(e);
        }
      } else
        print("Sender ID null");
    } else
      print("Sender null");
  }

If you can also provide me some informations about StreamBuilder and Stream in general, I would be very happy to listen but obviously this is a plus.

Solved, the problem was the Stream, I wasn't returning the right one but with QuerySnapshot it works:

Stream<QuerySnapshot> streamChats(User sender) {
    return FirebaseFirestore.instance
        .collection("ids")
        .doc("tabs")
        .collection(sender.id[0].toLowerCase())
        .doc(sender.id[0].toLowerCase())
        .collection("chats")
        .snapshots();
  }

You need to create ChatCard objects from your stream, , assuming you want them in a list, because you are getting a list of docs , and return them.

Stream<List<ChatCard>> streamChats(MyUser sender) {
List<ChatCard> chatCardList=[];

    if (sender != null) {
      if (sender.id != null) {
        try {
         return await FirebaseFirestore.instance
              .collection("${sender.id}/chats'")
              .snapshots().then((snap){
           for(var item in snap.data.docs){
             chatCardList.add(ChatCard.fromMap(snap.data));}
          return chatCardList; });
          print(
              "${sender.id}/chats'");
        } on FirebaseException catch (e) {
          print(e);
        } catch (e) {
          print(e);
        }
      } else
        print("Sender ID null");
    } else
      print("Sender null");
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM