Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Fetch firebase stream with cubit

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Can any one help me please?

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading