1. 产生的原因
-
我在action里面定义了一个login 事件
enum LoginAction { login,//登录事件 } class LoginActionCreator { static Action onLogin(){ return const Action(LoginAction.login); } }
-
在effect里面处理了这个登录事件
Effect<LoginState> buildEffect() { return combineEffects(<Object, Effect<LoginState>>{ LoginAction.login: _onLogin, ///这个是登录事件的处理 }); } ///这个是登录事件的处理 void _onLogin(Action action, Context<LoginState> ctx) async { Navigator.of(ctx.context).pushNamed('customer', arguments: null); }
-
运行后发现reducer 里面接收不到这个事件
Reducer<LoginState> buildReducer() { return asReducer( <Object, Reducer<LoginState>>{ LoginAction.login : _login, }, ); } //不执行??? LoginState _login(LoginState state, Action action) { final LoginState newState = state.clone(); return newState; }
-
原因
因为在effect里面已经处理过这个事件了,所以reducer里面就接收不到这个事件了,可以理解为effect已经消费过这个事件,不在往下传递这里借助别人的一张fish_redux 事件流程图,方便大家理解
解决办法
在effect 响应的方法里面,在dispatch一个事件,发送到reducer里面
ctx.dispatch(CustomerActionCreator.onSetListData(items));