Bloc vs StreamBuilder for a Flutter chat app

Viewed 1086

I'm building the chat part of my app using firbase, and I am thinking about its architecture.

enter image description here

I'm thinking about either :

  • using the BLoC pattern and so, subscribe to the stream of messages (or the list of conversations for the second screen) inside the bloc, and emmiting a new state with the list of messages (or conversations) everytime a message comes from the stream. That way, the BlocBuilder of the UI will rebuild the UI everytime a new message in sent.
  • Directly plug the stream to a StreamBuilder in the UI, and therefore not using the BLoC pattern

What would be the difference between these approaches ?

1 Answers

I'll go for Bloc without any doubts but that's only my point of view. StreamBuilder is easier and faster to write and faster but at some point it will become a mess in your code and there are strict rules that you should respect if you don't want to end up with an infinite loop. If you are using bloc, then you can use a StatelessWidget and have a better controle of the rebuild. I have written a story about that on medium : https://arnaudelub.medium.com/flutter-bloc-and-firestore-stream-the-perfect-match-if-used-the-right-way-5a4607f093a2

Related