Effect

    • Effect 是一个处理所有副作用的函数。它接收下面的参数
      • Action action
      • Context context
        • BuildContext context
        • T state
        • dispatch
        • isDisposed
    • 它主要包含四方面的信息
      • 接收来自 View 的“意图”,包括对应的生命周期的回调,然后做出具体的执行。
      • 它的处理可能是一个异步函数,数据可能在过程中被修改,所以我们应该通过 context.state 获取最新数据。
      • 如果它要修改数据,应该发一个 Action 到 Reducer 里去处理。它对数据是只读的,不能直接去修改数据。
    • 示例代码
    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. }