BLoC stands for Business Logic Component. It’s an observer pattern where data comes in from BLoC, comes out of Stream, and updates widgets
In short, the cautions and benefits of using BLoC are:
- Shifting Business Logic to one or more BLoC;
- Separate it from the Presentation Layer as much as possible; in other words, the UI component only needs to care about the UI, not the business logic;
- Input (Sink) and output (Stream) depend only on the use of Streams;
- Maintain platform independence;
- Environmental independence is maintained.
- High code reuse
Most important: Each page can have its own BLoc
Every time a new page is created, such as a login page, a new BLoc must be created, and the BLoc simply takes the data and injects it into the page to be displayed to the user.
Reprinted in other articles
Dart each module has xxx_Bloc. Dart, xxx_state. Dart, xxx_Event.dart
Dart extends <xxx_event.dart,xxx_state.dart>,
Dart is a collection of states including (initial state, failed data load, normal data load)
Xxx_event. dart calls the corresponding method for retrieving data based on the event.
If (state is xxxxState) {}
Use BlocProvider BlocBuilder<xxxxBloc, xxxxState>(Builder: (context, state) {}