【问题标题】:Navigation doesn't work from signUp to Home.[Flutter]从 signUp 到 Home 的导航不起作用。[Flutter]
【发布时间】:2020-10-08 04:10:08
【问题描述】:

当我在创建用户后创建新用户时,它不会在身份验证状态更改时导航到主页。但是如果我刷新应用程序,那么它只会进入主页。

但是,如果我登录已经注册的用户,那么它会按预期导航到主页。

问题是注册后需要进入首页。

这就是我所做的:

  1. MyApp 返回我想要执行逻辑的包装器。它有一个流构建器,可以在身份验证状态更改时导航到相应的页面。
class Wrapper extends StatefulWidget {
  @override
  _WrapperState createState() => _WrapperState();
}

class _WrapperState extends State<Wrapper> {
  @override
  Widget build(BuildContext context) {
    return StreamBuilder(
      stream: FirebaseAuth.instance.authStateChanges(),
      builder: (context, snapshot) {
        if (snapshot.connectionState == ConnectionState.active) {
          User user = snapshot.data;
          if (user == null) {
            return Authenticate();
          } else {
            print(user);
            print("going to home page......");
            return HomePage();
          }
        } else {
          return Scaffold(
            body: Center(
              child: CircularProgressIndicator(),
            ),
          );
        }
      },
    );
  }
  1. 我的注册逻辑。
 Future registerUser(String email, String password) async {
    try {
      UserCredential userCredential = await _firebaseAuth
          .createUserWithEmailAndPassword(email: email, password: password);
      User user = userCredential.user;
      return _userFromFirebase(user);
    } catch (e) {
      print(e.toString());
      return null;
    }
  }

我这里只是根据用户模型映射用户。

【问题讨论】:

  • 不要使用 authStateChanges,尝试使用 currentUser()
  • 我试过了,还是不行。

标签: flutter firebase-authentication


【解决方案1】:

您是否尝试将检查状态更改为snapshot.connectionState == ConnectionState.done &amp;&amp; snapshot.data != null?我认为ConnectionState.active 与ConnectionState.done 的状态不同

https://api.flutter.dev/flutter/widgets/ConnectionState-class.html

【讨论】:

    猜你喜欢
    • 2019-01-28
    • 2020-07-31
    • 2022-01-18
    • 2016-02-03
    • 2020-04-08
    • 2021-12-08
    • 2020-09-05
    • 2023-02-10
    • 2020-03-02
    相关资源
    最近更新 更多