Effect

    • Effect is a function that handles all side effects. It receives the following parameters
      • Action action
      • Context context
        • BuildContext context
        • T state
        • dispatch
        • isDisposed
    • It mainly contains four aspects of information
      • Receive "intent" from the View, including the corresponding lifecycle callback, and then make specific execution.
      • Its processing may be an asynchronous function, the data may be changed in the process, so we should get the latest data through context.state.
      • If you want to modify the data, you should send an Action to the Reducer to handle. It is read-only for data and cannot be modified directly in a effect function.
    • Sample Code
    1. /// another style of writing
    2. Effect<String> buildEffect() {
    3. return combineEffects(<Object, Effect<String>>{
    4. Lifecycle.initState: _initState,
    5. 'onShare': _onShare,
    6. });
    7. }
    8.  
    9. void _initState(Action action, Context<String> ctx) {
    10. //do something on initState
    11.  
    12. void _onShare(Action action, Context<String> ctx) async {
    13. //do something on onShare
    14. await Future<void>.delayed(Duration(milliseconds: 1000));
    15. ctx.dispatch(const Action('shared'));
    16. }
    17.  
    18. class MessageComponent extends Component<String> {
    19. MessageComponent(): super(
    20. view: buildMessageView,
    21. effect: buildEffect(),
    22. }