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. Effect<String> buildEffect() {
    2. return combineEffects(<Object, Effect<String>>{
    3. Lifecycle.initState: _initState,
    4. 'onShare': _onShare,
    5. });
    6. }
    7. //do something on initState
    8. void _onShare(Action action, Context<String> ctx) async {
    9. //do something on onShare
    10. await Future<void>.delayed(Duration(milliseconds: 1000));
    11. ctx.dispatch(const Action('shared'));
    12. class MessageComponent extends Component<String> {
    13. MessageComponent(): super(
    14. view: buildMessageView,
    15. effect: buildEffect(),
    16. }