Home > Enterprise >  Fetch firebase stream with cubit
Fetch firebase stream with cubit

Time:05-30

I'm fairly new to flutter and I have a question regarding Firebase database.

I'm trying to create a chat app, and I want to fetch messages automatically when a new message is sent by any of the chat members.

I'm using bloc/cubit for state management.

Can any one help me please?

CodePudding user response:

So regarding fetching stream of messages from Firebase real-time database, I'm guessing you will be using firestore database, So my suggestion is doing the following

    emit(FetchAllMessagesLoadingState());

    FirebaseFirestore.instance
        .collection("messages") // or whatever your collection name is
        .snapshots()
        .listen((event) {
      
      event.docs.forEach((element) {
        // Parse your messages here, may be add them to a list
      });
      emit(FetchAllMessagesSuccessState());
    });

hope this answer can be helpful for you

  • Related