StreamBuilder, Flutter and Firebase

42 Views Asked by At

I'm making a Reddit clone and I've just finished the chat part. Everytime I send a message to someone, the database is structured in this way: users/$userid/chats/$receiverid/messages/$allmessagesmodels. Now I want to display the list of all the recent chats with a StreamBuilder, but it seems there is no data.

Stream<List<UserModel>> getUserChats() {
  final currentId = authInstance.currentUser!.uid;

  return firestoreInstance.collection("users").doc(currentId).collection("chats").snapshots().map((docs) => docs.docs.map((doc) => UserModel.fromMap(doc.data())).toList(),);

}

And this is the chat part where I use the StreamBuilder.

class ChatTab extends ConsumerStatefulWidget {
  const ChatTab({super.key});

  @override
  ConsumerState<ChatTab> createState() => _ChatTabState();
}

class _ChatTabState extends ConsumerState<ChatTab> {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: secondaryBgColor,
      body: Container(
        child: StreamBuilder(
          stream: ref.watch(chatControllerProvider).getUserChats(),
          builder: (context, snapshot) {
            if(snapshot.hasError) {
              log(snapshot.error.toString());
              return new Text('Error: ${snapshot.error}', style: TextStyle(color: Colors.black),);
            }

            return SizedBox();
          },
        ),
      ),
    );
  }
}

I'm sure that the chats exist and the database structure is right, but I don't see nothing, If i try to log the data, it prints me: "[log] []" . I also tried to log the snapshot.error, but there is no error.

0

There are 0 best solutions below