微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

firebase登录后如何自动重定向?

如何解决firebase登录后如何自动重定向?

我按照本教程 https://www.youtube.com/watch?v=oJ5Vrya3wCQ 对我的应用实施了 Firebase 身份验证。

除了一件事外,一切都很好。当我在登录屏幕并单击登录按钮时,我必须重新启动应用程序才能访问主页。 当我点击注销时不会发生这种情况,我会立即重定向登录

这是我的登录

  Future<String> signIn({String email,String password}) async {
    try {
      await _firebaseAuth.signInWithEmailAndPassword(
          email: email,password: password);
      return "Connecté";
    } on FirebaseAuthException catch (e) {
      return e.message;
    }
  }

main.dart

class AuthenticationWrapper extends StatelessWidget {
  const AuthenticationWrapper({
    Key key,}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final firebaseUser = context.watch<User>();

    if (firebaseUser != null) {
      return HomePage();
    }

    return IntroScreen();
  }
}

我的升高按钮:

ElevatedButton(
                                onpressed: () {
                                  context.read<Authentication>().signIn(
                                      email: emailController.text.trim(),password: passwordController.text.trim());
                                },child: Text('Se connecter'),style: ButtonStyle(
                                  backgroundColor:
                                      MaterialStateProperty.all<Color>(
                                          Color(0xff0d47a1)),shape: MaterialStateProperty.all<
                                          RoundedRectangleBorder>(
                                      RoundedRectangleBorder(
                                          borderRadius:
                                              BorderRadius.circular(40))),))

解决方法

实际上你并没有在用户登录后在主屏幕添加导航器。

在 Elevated 按钮的 onPressed

onPressed: () {
       await context.read<Authentication>().signIn(
             email: emailController.text.trim(),password: passwordController.text.trim());
       Navigator.push(context,MaterialPageRoute(builder: (context) => HomePage()));
       },

这会奏效。

,

示例代码是

bool invalid = false;
  String password;
  String error = "";
  loginUser() async {
    print(error);
    try {
      await FirebaseAuth.instance
          .signInWithEmailAndPassword(email: email,password: password);
    } on FirebaseAuthException catch (e) {
      error = e.toString();
    }
    FirebaseAuth.instance.authStateChanges().listen((user) {
      if (error.isNotEmpty) {
        setState(() {
          invalid = true;
        });
      } else {
        setState(() {
          Navigator.popAndPushNamed(context,"homePage");
        });
      }
    });
  }

就在登录按钮上方

if (error.isNotEmpty)
            Row(mainAxisAlignment: MainAxisAlignment.center,children: [
              if (error.split(" ").contains("password"))
                Text(
                  "Invalid Password",style: TextStyle(color: Colors.red),),if (error.split(" ").contains("record"))
                Text(
                  "User Does Not Exist",]),

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。