【问题标题】:Flutter, Redux and Firebase Auth Invalid argument(s) errorFlutter、Redux 和 Firebase Auth Invalid argument(s) 错误
【发布时间】:2019-05-25 06:59:02
【问题描述】:

我正在开发 Flutter 应用程序,并将 Firebase 身份验证添加到项目中。 Google 登录运行良好,但我不明白 make 是如何工作的 使用电子邮件和密码登录。

我按照https://flutterbyexample.com/log-in-redux-cycle-contd 那里的示例进行操作。

我添加了操作、reducers 和中间件,遵循 Google 登录指南,但在电子邮件和密码情况下发生了一些奇怪的情况。

这里是日志:

I/FirebaseAuth(27942): [FirebaseAuth:] Loading module via FirebaseOptions.
I/FirebaseAuth(27942): [FirebaseAuth:] Preparing to create service connection to gms implementation
D/FirebaseAuth(27942): Notifying id token listeners about user ( MZr9euoZKEbWWqNIrB5OcZIWcwf2 ).
D/FirebaseApp(27942): Notifying auth state listeners.
D/FirebaseApp(27942): Notified 0 auth state listeners.
I/flutter (27942): [INFO] LoggingMiddleware: {Action: LogInWithMailAndPasswordFail{There was an error loggin in: Invalid argument(s)}, State: AppState{isLoading: false, currentUser: null}}, ts: 2018-12-27 09:03:18.185480}
I/flutter (27942): [INFO] LoggingMiddleware: {Action: Instance of 'LogInWithMailAndPassword', State: AppState{isLoading: false, currentUser: null}}, ts: 2018-12-27 09:03:18.199120}

正如您在现实中看到的那样,登录有效,但在登录后立即调用 LogInWithMailAndPasswordFail 操作。

这里是部分中间件代码:

    Middleware<AppState> _createLogInWithMailAndPasswordMiddleware() {
  // These functions will always take
  // your store,
  // the action thats been dispatched
  // and the a special function called next.
  return (Store store, action, NextDispatcher next) async {
    // FirebaseUser is the type of your User.
    FirebaseUser user;
    // Firebase 'instances' are temporary instances which give
    // you access to your FirebaseUser. This includes
    // some tokens we need to sign in.
    final FirebaseAuth _auth = FirebaseAuth.instance;

    if (action is LogInWithMailAndPassword) {
      try {

        user = await _auth.signInWithEmailAndPassword(
              email: action.getUsername(),
              password: action.getPassword());


        print('Logged in ' + user.displayName);
        // This can be tough to reason about -- or at least it was for me.
        // We're going to dispatch a new action if we logged in,
        //
        // We also continue the current cycle below by calling next(action).
        store.dispatch(new LogInWithMailAndPasswordSuccessful(user: user));
      } catch (error) {
        store.dispatch(new LogInWithMailAndPasswordFail(error));
      }
    }

    // After you do whatever logic you need to do,
    // call this Redux built-in method,
    // It continues the redux cycle.
    next(action);
  };
}

【问题讨论】:

    标签: firebase redux firebase-authentication flutter


    【解决方案1】:

    我会检查user 对象是否具有displayName 属性。

    通过打印user 对象而不是user.displayName 进行验证

    【讨论】:

      【解决方案2】:

      我的问题是我没有正确实例化中间件,因为在本节中我有多个中间件。

      List<Middleware<AppState>> createAuthMiddleware()  {
        print('createAuthMiddleware');
        final logIn = createLogInMiddleware();
        final logOut = createLogOutMiddleware();
        final signUp = createSignUpMiddleware();
        return [
          TypedMiddleware<AppState, LogInWithGoogle>(logIn),
          TypedMiddleware<AppState, LogInWithFacebook>(logIn),
          TypedMiddleware<AppState, LogInEmailAndPassword>(logIn),
          TypedMiddleware<AppState, CheckLogIn>(logIn),
          TypedMiddleware<AppState, ForgotPassword>(logIn),
          TypedMiddleware<AppState, SignUpEmailAndPassword>(signUp),
          TypedMiddleware<AppState, LogOut>(logOut),
        ];
      }
      

      【讨论】: