Flutter Redux中间件显示为空

时间:2018-10-28 16:20:28

标签: flutter flutter-redux

middleware.dart

MiddlewareHandler<AppState, AppStateBuilder, AppActions, Null> createFetchUser(
    Repository repository) {
  return (MiddlewareApi<AppState, AppStateBuilder, AppActions> api, ActionHandler next, Action<Null> action) {
    if (api.state.isLoading) {
      repository.loadUser().then((user) {
        print('Middleware User' + user.toString()); //problem is somewhere here
        api.actions.loadUserSuccess(user);
      }).catchError((e) {
        print('No user found. Please login');
      });
    }
    next(action);
  };
}

repository.dart

  Future<User> loadUser() async {
    try {
        return await userStorage.loadUser();
    } catch (e) {
      print('Repository_Error'+e.toString());
    }
  }

user_storage.dart

Future<User> loadUser() async {
   final file = await _getLocalFile();
   final contents = await file.readAsString();
   print('Contents=' + contents);
   return serializers
          .deserializeWith(AppState.serializer, json.decode(contents))
          .currentUser;
  }

输出:

I/flutter ( 5718): File: '/data/user/0/com.projec.rep/app_flutter/FlutterMvcUserStorage____built_redux_user_app__.json'
I/flutter ( 5718): Contents={"user":{"token":"17389e0d58d138f6480bb077df8d6eb2eb6412254ffcbd2827f321453ad68447","isLoading":false}}
I/flutter ( 5718): Middleware User=null
I/flutter ( 5718): No user found. Please login

我在repository.dart和user_storage.dart中都得到了响应,但是在中间件中却为“ null”。有人可以帮助我在代码中找到问题区域吗?

0 个答案:

没有答案