I am new to flutter and the famous flutter_bloc library. I have created a chat system based on the BLoC pattern :
- A widget fires an event to chatBloc
- The chatBloc catches the event, process it and call chatRepository
- ChatRepository adds the message as a draft in its messageList variable and then sends the message to the API
- ChatRepository receives the message from the API when it is done and replaces the draft within its messageList variable
- ChatBloc yields its final state and the page rebuilds
Everything works fine as long as no more than one message is processed at a time.
If my button widget fires two quick events, they will overlap and so the second event will fire while the first event is still in progress. By the time when the second event starts, the first message is still a draft, so when message 2 completes and chatRepository updates its messageList, it will treat message 1 as a draft even if it has been validated.
At this point I'm asking myself if I made a mistake or if I'm trying to do it the wrong way. Would it be better to create a queue and process messages in the background one by one ?
Thank you for your help !