sequence

In Flutter, there is the concept of state. Bloc is used for state management, and events are transmitted through Bloc. AddEvent to change the state. For the basic usage of Bloc, please refer to the information on Bloc’s official website. Here we only record the queuing of Bloc.

First, let’s create a new Bloc class:

class TestBloc extends Bloc<TestEvent, TestState> {
  TestBloc() : super(new TestState());

  @override
  Stream<TestState> mapEventToState(TestEvent event) async* {
    if (event is TestEventA) {
      print("A");
      await Future.delayed(Duration(seconds: 2));
      print("finish A await");
    } else if (event is TestEventB) {
      print("B");
    }
  }
}

abstract class TestEvent {}

class TestEventA extends TestEvent {}

class TestEventB extends TestEvent {}

class TestState {}
Copy the code

Above, A basic Bloc is defined, event A, event B. After receiving Event A, we delayed for 2 seconds to verify whether the Event Loop of Bloc was obstructed.

Dart and add button events and send both events A and B to Bloc:

/ /... Omit non-critical onPressed: (){_bloc. Add (TestEventA()); _bloc.add(TestEventB()); }Copy the code
Phenomenon:

Print (“A”) prints, waits 2 seconds, prints “Finish A await”, and then responds to event B.

Conclusion:

The event queue of Bloc is an obstructive queue, first-in, first-out, and the next event in the response queue will be triggered only after the consumption of the previous event is completed.


Explain the symptoms of problems encountered during development:

Before await, addEvent will block the thread and cause the receiving delay of the event, but before await, Yeild will not be affected and can receive the state change before delay

Is the Stream queue also ordered?

Through the code test, the order of the message body in the flow has nothing to do with the order of joining, but has a positive correlation with the time of the message body itself.