Flutter:如何使用 Firebase PhoneNumberAuth 修复丢失与设备的连接错误

Posted

技术标签:

【中文标题】Flutter:如何使用 Firebase PhoneNumberAuth 修复丢失与设备的连接错误【英文标题】:Flutter : how to fix Lost connection to device error with firebase PhoneNumberAuth 【发布时间】:2020-01-28 08:18:42 【问题描述】:

我正在实施 phoneNumberAuth 注册。但是有一个问题

当我点击 authbottom 时,终止 ios 应用程序

代码:

String smsCode, verificationId;

  Future<void> verifyPhone() async 
    final PhoneCodeAutoRetrievalTimeout autoRetrieve = (String verId) 
      this.verificationId = verId;
    ;

    final PhoneCodeSent smsCodeSent = (String verId, [int forceCodeResend]) 
      this.verificationId = verId;
      print('asd');
      smsCodeDialog(context).then((value) 
        print('Signed in');
      );
    ;

    final PhoneVerificationCompleted verificationCompleted = (AuthCredential credential) 
      print('verified');
    ;

    final PhoneVerificationFailed verfiFailed = (AuthException exception) 
      print('$exception.message+ddddddddddd');
    ;

    await FirebaseAuth.instance.verifyPhoneNumber(
      phoneNumber: this.phoneNo,
      timeout: const Duration(seconds: 5),
      verificationCompleted: verificationCompleted,
      verificationFailed: verfiFailed,
      codeSent: smsCodeSent,
      codeAutoRetrievalTimeout: autoRetrieve,
    );
  

  Future<bool> smsCodeDialog(BuildContext context) 
    return showDialog(
        context: context,
        barrierDismissible: false,
        builder: (BuildContext context) 
          return AlertDialog(
            title: Text('SMS 코드를 입력해주세요'),
            content: TextField(
              onChanged: (value) 
                this.smsCode = value;
              ,
            ),
            contentPadding: EdgeInsets.all(10),
            actions: <Widget>[
              FlatButton(
                child: Text('Done'),
                onPressed: () 
                  FirebaseAuth.instance.currentUser().then((user) 
                    if (user != null) 
                      Navigator.of(context).pop();
                      Navigator.of(context).pushReplacementNamed('/');
                     else 
                      Navigator.of(context).pop();
                      SIGNIn();
                    
                  );
                ,
              )
            ],
          );
        );
  



  SIGNIn() async
    final AuthCredential credential = PhoneAuthProvider.getCredential(
      verificationId: verificationId,
      smsCode: smsCode,
    );
    print('진행중');
    FirebaseAuth _auth = FirebaseAuth.instance;
    final FirebaseUser user = (await _auth.signInWithCredential(credential)).user;
    final FirebaseUser currentUser = await _auth.currentUser();
    assert(user.uid == currentUser.uid);
    setState(() 
      if(user != null)
        print('success!');
      else
        print('sign in failed');
      
    );
  

错误代码:


*** First throw call stack:
(
    0   CoreFoundation                      0x00000001169bb8db __exceptionPreprocess + 331
    1   libobjc.A.dylib                     0x0000000115f66ac5 objc_exception_throw + 48
    2   CoreFoundation                      0x00000001169d9c94 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
    3   CoreFoundation                      0x00000001169c0623 ___forwarding___ + 1443
    4   CoreFoundation                      0x00000001169c2418 _CF_forwarding_prep_0 + 120
    5   Runner                              0x000000010e97d966 -[FIRPhoneAuthProvider internalVerifyPhoneNumber:UIDelegate:completion:] + 118
    6   Runner                              0x000000010e97ccc0 __64-[FIRPhoneAuthProvider verifyPhoneNumber:UIDelegate:completion:]_block_invoke + 272
    7   libdispatch.dylib                   0x0000000115e16ccf _dispatch_call_block_an<…>
Lost connection to device.

如何修复我的代码?

直到有人帮助我,我才尝试修复

这很难,因为解释错误不多。

如果有人能帮助我,我将不胜感激。

有人帮帮我:(

【问题讨论】:

【参考方案1】:

pop 删除***小部件。不确定在此之后放置逻辑是否是个好主意。最好重新安排你的代码,比如

  // Only gets SMS, no functionality
  Future<String> getSmsCode(BuildContext context) 
    return showDialog<String>(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext context) 
        return AlertDialog(
          title: Text('SMS 코드를 입력해주세요'),
          content: TextField(
            onChanged: (value) 
              this.smsCode = value;
            ,
          ),
          contentPadding: EdgeInsets.all(10),
          actions: <Widget>[
            FlatButton(
              child: Text('Done'),
              onPressed: () 
                Navigator.of(context).pop(this.smsCode);
              ,
            )
          ],
        );
      ,
    );
  


  SIGNIn() async 
    String smsCode = await getSmsCode(context);
    if (smsCode != null && !smsCode.isNotEmpty) 
      print('User cancelled SMS dialog');
      return;
    
    final AuthCredential credential = PhoneAuthProvider.getCredential(
      verificationId: verificationId,
      smsCode: smsCode,
    );
    print('진행중');
    FirebaseAuth _auth = FirebaseAuth.instance;
    final FirebaseUser user = (await _auth.signInWithCredential(credential)).user;
    final FirebaseUser currentUser = await _auth.currentUser();
    assert(user.uid == currentUser.uid);
    setState(() 
      if (user != null) 
        print('success!');
       else 
        print('sign in failed');
      
    );
  

现在只调用SIGNIn,它将首先获取短信代码,然后使用该短信代码登录。希望对您有所帮助。

【讨论】:

太好了,你区分注册和登录的逻辑是什么?如何找到用户 mob 是注册用户? signIn 方法的身份验证结果在内部有isNewUser(即AuthResult.AdditionalUserInfo.isNewUser)。您可以使用它来查找它是否是新用户 但我不使用它。我通常坚持用户,如果可用,它是现有用户。

以上是关于Flutter:如何使用 Firebase PhoneNumberAuth 修复丢失与设备的连接错误的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Firebase 在 Web 上托管 Flutter?它的效果如何?

如何在项目中使用 firebase 和 firebase 功能的现有 Flutter 应用程序中更新包名称?

如何使用 Firebase 正确配置 Flutter 应用程序?

如何使用 Flutter 执行 Firebase 可变事务数据?

如何使用 Flutter 和 Firebase 为聊天应用制定规则

Flutter + Firebase:如何实现“谷歌登录”