扑朔迷离的Redux:第二个中间件被两次调用了吗?

时间:2020-05-11 03:35:40

标签: flutter redux

我有2个中间件。一种用于验证,另一种用于API。首先,我会验证登录详细信息(如果它们不为null),然后再进行验证。我将在另一个中间件API中调度一个新动作。到(将详细信息发布到服务器)。问题是。 API中间件被调用两次?

请记住,每个中间件都有其自己的动作,状态和缩减器。我不知道这是否是问题,或者您不希望显示所有脚本,但我不确定它们是否重要。

这是2个中间件脚本:

validation_middleware.dart

class   ValidationMiddleware extends MiddlewareClass<AppState>{

  @override
  void call(Store<AppState> store, dynamic action, NextDispatcher next) {

    if ( action is LoginAction ) {
      loginAction(action.email, action.password, next);
    }
  }
    void loginAction(String email, String password, NextDispatcher next) {
      if(email != null && password != null)
      {
        print('email: ' + email);
        print('password: ' + password);
        next(new APILoginAction(email, password));
      }
    }
}

api_middleware.dart

class APIMiddleware extends MiddlewareClass<AppState> {

  @override
  void call(Store<AppState> store, dynamic action, NextDispatcher next) {

    print('APIMiddleware');

    if(action is APILoginAction) {
      print('if(action is APILoginAction)');
      apiLoginAction(action.email, action.password, next);
    }
  }
}

void apiLoginAction(String email, String password, NextDispatcher next) {
  print('email: ' + email);
  print('password: ' + password);
  //api stuff
}

日志:

I/flutter ( 3210): Login Button is Pressed
I/flutter ( 3210): email: asdfsadfdfdsfsddfdfdfsdfsdfsd
I/flutter ( 3210): password: fsdfdsfsdfsdfsdfds
I/flutter ( 3210): APIMiddleware
I/flutter ( 3210): if(action is APILoginAction)
I/flutter ( 3210): email: asdfsadfdfdsfsddfdfdfsdfsdfsd
I/flutter ( 3210): password: fsdfdsfsdfsdfsdfds
I/flutter ( 3210): APIMiddleware
I/flutter ( 3210): if(action is APILoginAction)
I/flutter ( 3210): email: asdfsadfdfdsfsddfdfdfsdfsdfsd
I/flutter ( 3210): password: fsdfdsfsdfsdfsdfds
正如您从日志APIMiddleware中看到的那样,

以相同的动作被调用了两次。我的语法是否正确?

0 个答案:

没有答案