【问题标题】:Unhandled Exception: 'package:firebase_auth/src/firebase_auth.dart'未处理的异常:'package:firebase_auth/src/firebase_auth.dart'
【发布时间】:2021-05-20 01:13:55
【问题描述】:
  Firebase Phone authentication in flutter,  
Phone authentication code was working perfectly for flutter firebase plugins- `firebase_auth: ^0.14.0+5`, `firebase_core: ^0.4.0+9`, `cloud_firestore: ^0.13.0+1

Now updated to- `firebase_auth: ^0.20.0+1`, `firebase_core: ^0.7.0`, `cloud_firestore: ^0.16.0` 

its throwing error: 
        
        >     Unhandled Exception: 'package:firebase_auth/src/firebase_auth.dart': Failed assertion: line 633 pos 12: 'codeAutoRetrievalTimeout != null': is not true

 
My Code
        
//Firebase Phone auth code
              Future<bool> loginUser(String phone ,BuildContext context) async{
                 FirebaseAuth _auth = FirebaseAuth.instance;
              final prefs = await SharedPreferences.getInstance();
              String name = prefs.getString('roleName');
              final bloc = BlocProvider.of<AppRrgisterationScreenBloc>(context);
              _auth.verifyPhoneNumber(
                  // phoneNumber: phone,
                  phoneNumber: '+91${phone}',
                  timeout: Duration(seconds: 0),
                  verificationCompleted: (AuthCredential credential) async{
                    Navigator.of(context).pop();
                    UserCredential result = await _auth.signInWithCredential(credential);
                    User user = result.user;
                    if(user != null){
                      contactnumberVal=phone;
                      validateAndCreateData(bloc,context);
                    }else{
                      print("Error");
                    }
            
//This callback would gets called when verification is done automaticlly
                  },
                  verificationFailed: (FirebaseAuthException exception){
                    print(exception);
                  },
                  codeSent: (String verificationId, [int forceResendingToken]){
                    showDialog(
                        context: context,
                        barrierDismissible: false,
                        builder: (context) {
                          return AlertDialog(
                            title: Text("Please enter the verification code sent to the mobile number and select Confirm",style: TextStyle(
                              fontSize: 16.0,
                            )),
                            content:Form(
                              key: _resetKey,
                              autovalidate: _validate,
                              child: Column(
                                mainAxisSize: MainAxisSize.min,
                                children: <Widget>[
                                  TextFormField(
                                    keyboardType: TextInputType.number,
                                    controller: verificationcode,
                                  ),
                                ],
                              ),
                            ),
                            actions: <Widget>[
                              showErrorMessageVerificationCode ?
                              Container(
                                  decoration: BoxDecoration(
                                    
                                   borderRadius: BorderRadius.circular(80.0)
                                  ),
                                  child: Padding(
                                      padding: EdgeInsets.all(10.0),
                                      child: Text('Code is Requried!',style: TextStyle(color:Color( 0xFFE9140B),fontSize: 16.0),)
                                  )
                              )
                                  : Container(),
                              SizedBox(height: 5.0),
                              FlatButton(
                                child: Text("Cancel"),
                                textColor: Colors.white,
                                color: Color(0xFFE9140B),
                                onPressed:   () => Navigator.pop(context),
                              ),
                              FlatButton(
                                child: Text("Confirm"),
                                textColor: Colors.white,
                                color: Color(0xFFE9140B),
                                onPressed: () async{
                                  
                                  final code = verificationcode.text.trim();
                                  AuthCredential credential = PhoneAuthProvider.credential(verificationId: verificationId, smsCode: code);
            
                                   _auth.signInWithCredential();
                                  UserCredential result = await _auth.signInWithCredential(credential).catchError((error) {
                                    print("error");
                                    Navigator.pushAndRemoveUntil(context, MaterialPageRoute(
                                        builder: (context) => MobileAuthFails(names:name,phone:phone,)
                                      
                                      ),
                                          ModalRoute.withName(""));
            
                                   
                                  });
            
  //Role base sign up
                                  User user = result.user;
                                  if(user != null){
                                    contactnumberVal=phone;
                                    SharedPreferences prefs = await SharedPreferences.getInstance();
                                    prefs.setString('reg_number', phone);
                                    prefs.setString('phoneNumber', phone);
                                    validateAndCreateData(bloc,context);
            
                                    Navigator.pushAndRemoveUntil(context, MaterialPageRoute(
                                        builder: (context) =>
                                            MyAppEligiblity(phone:phone)
                                      // ResetPassword(user: user,)
                                    ),
                                        ModalRoute.withName(""));
                                    prefs.setBool('IsLogin', true);
                                  }
                                  else{
                                    print("error");
                                  }
                                },
                              )
            
                            ],
            
                          );
                        }
                    );
                  },
            
                codeAutoRetrievalTimeout: null,
                  // codeAutoRetrievalTimeout: (String verificationId){
                  //   verificationId = verificationId;
                  //   print(verificationId);
                  //   print("Timout");         }
              );
              }
          
        

1.verificationCompleted:自动处理Android设备上的短信代码。

2.verificationFailed:处理电话号码无效或是否超出短信配额等失败事件。

3.codeSent:当Firebase向设备发送代码时处理,用于提示用户输入代码。

4.codeAutoRetrievalTimeout:自动短信代码处理失败时的超时处理。

【问题讨论】:

  • 请在进行身份验证的地方分享您的代码。
  • 我已经发布了代码@PreetShah

标签: flutter


【解决方案1】:

您不能将codeAutoRetrievalTimeout 设为空。它需要你在那里传递一个函数。此函数用于在自动代码检索超时时执行操作。所以,如果你对这个函数没有任何用处,请放一个空函数如下:

codeAutoRetrievalTimeout: (String verificationId) {}

您可以在here 上找到示例代码和其他文档,这非常有帮助。

另外,请确保您在下次遇到任何问题时分享您的代码和错误日志。

【讨论】:

  • Flutter 应用程序在添加 codeAutoRetrievalTimeout 后崩溃:(String verifyId) {} E/AndroidRuntime(17787): FATAL EXCEPTION: main E/AndroidRuntime(17787): Process: com.example.jeevarakta_app, PID: 17787 E/AndroidRuntime(17787): java.lang.NoClassDefFoundError: 解析失败@PreetShah
  • 您需要与我分享整个错误日志。这么多也无济于事。将其添加到问题本身,并确保添加所做的更改。
猜你喜欢
  • 1970-01-01
  • 2011-11-19
  • 1970-01-01
  • 1970-01-01
  • 2011-01-16
相关资源
最近更新 更多