0

I am new to flutter and the famous flutter_bloc library. I have created a chat system based on the BLoC pattern :

  1. A widget fires an event to chatBloc
  2. The chatBloc catches the event, process it and call chatRepository
  3. ChatRepository adds the message as a draft in its messageList variable and then sends the message to the API
  4. ChatRepository receives the message from the API when it is done and replaces the draft within its messageList variable
  5. 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 !

Tolkar
  • 34
  • 7
  • You should probably adapt a different error handling, if you notice some of the chat apps, they tend to indicate the status for each message sent, when that message fails to be delivered it shows an error. May be you should try that ? – GoodSp33d Nov 01 '20 at 14:23
  • Well there's no actual error to catch and report to users. The current problem is that the repository instance within chatBloc is not updating its messageList as expected. – Tolkar Nov 01 '20 at 16:47

0 Answers0