【问题标题】:Flutter : Firebase PhoneAuthentication Problem颤振:Firebase 电话身份验证问题
【发布时间】:2021-03-22 19:23:14
【问题描述】:

我刚刚开发了一个需要电话身份验证的应用程序。在登录屏幕内,我可以实现通过电话登录。但我担心的是:当我第一次输入电话号码并输入验证号码时,它会返回登录,实际上预计会导航到主屏幕。对于第二次尝试,系统能够按预期工作并导航到主屏幕。这是我的代码块。我想知道由于登录信息再次弹出并且系统在第二次尝试后能够导航到主屏幕,我犯了哪一部分代码:

我的代码块:

        class _LoginScreenState extends State<LoginScreen> {
           String phoneNo, smssent, verificationId;

           get verifiedSuccess => null;

        Future<void> verifyPhone() async {
         final PhoneCodeAutoRetrievalTimeout autoRetrieve = (String verId) {
         this.verificationId = verId;
            };
         final PhoneCodeSent smsCodeSent = (String verId, [int forceCodeResent]) {
         this.verificationId = verId;
         smsCodeDialoge(context).then((value) {
         print("Doğrulama Kodu Gönderildi");
            });
              };
         final PhoneVerificationCompleted verifiedSuccess = (AuthCredential auth) {};
         final PhoneVerificationFailed verifyFailed = (AuthException e) {
         print('${e.message}');
              };
          await FirebaseAuth.instance.verifyPhoneNumber(
          phoneNumber: phoneNo,
          timeout: const Duration(seconds: 5),
          verificationCompleted: verifiedSuccess,
          verificationFailed: verifyFailed,
          codeSent: smsCodeSent,
          codeAutoRetrievalTimeout: autoRetrieve,
              );
                }

            Future<bool> smsCodeDialoge(BuildContext context) {
            return showDialog(
              context: context,
              barrierDismissible: false,
              builder: (BuildContext context) {
                return new AlertDialog(
                title: Text('Doğrulama Kodunu Giriniz'),
                content: TextField(
                onChanged: (value) {
                this.smssent = value;
                   },
                     ),
                  contentPadding: EdgeInsets.all(10.0),
                  actions: <Widget>[
                     FlatButton(
                        onPressed: () {
                         FirebaseAuth.instance.currentUser().then((user) {
                          if (user != null) {
                            Navigator.of(context).pop();
                            Navigator.push(
                                        context,
                          MaterialPageRoute(builder: (context) => HomeScreen()),
                             );
                           } else {
                           Navigator.of(context).pop();
                               signIn(smssent);
                            }
                              });
                                  },
                               child: Text(
                                 'Doğrulama Yap',
                                 style: TextStyle(color: Colors.blue),
                                     ),
                                        ),
                                           ],
                                              );
                                                 });
                                                     }

                     Future<void> signIn(String smsCode) async {
                     final AuthCredential credential = PhoneAuthProvider.getCredential(
                     verificationId: verificationId,
                     smsCode: smsCode,
                        );

                                                                                         
          await     FirebaseAuth.instance.signInWithCredential(credential).then((user)               
                  { 
                  Navigator.push(
                   context,
                    MaterialPageRoute(
                     builder: (context) => LoginScreen(),
                         ),
                              );
                              }).catchError((e) {
                            print(e);
                                 });
                                       }

【问题讨论】:

    标签: firebase flutter dart firebase-authentication navigation


    【解决方案1】:
     if (user != null) {
                                Navigator.of(context).pop();
                                Navigator.push(
                                            context,
                              MaterialPageRoute(builder: (context) => HomeScreen()),
                                 ); 
    

    这里你调用pop,如果用户不为null,它将带你到前一个屏幕,然后你将HomeScreen压入堆栈。试着不要弹出,只是推送,顺便说一下,你在代码中使用路由和流提供程序吗?

    【讨论】:

      猜你喜欢
      • 2020-06-06
      • 1970-01-01
      • 2020-04-19
      • 2021-06-12
      • 2020-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-04
      相关资源
      最近更新 更多