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

kil*_*ale 5 authentication firebase flutter

我正在实施 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');
      }
    });
  }
Run Code Online (Sandbox Code Playgroud)

错误代码 :


*** 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.

Run Code Online (Sandbox Code Playgroud)

我该如何修复我的代码?

在有人帮助我之前,我正在尝试修复

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

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

来人帮帮我 :(

Che*_*ddy 2

pop删除顶级小部件。不确定将逻辑放在后面是个好主意。最好重新安排你的代码,比如

\n\n
  // Only gets SMS, no functionality\n  Future<String> getSmsCode(BuildContext context) {\n    return showDialog<String>(\n      context: context,\n      barrierDismissible: false,\n      builder: (BuildContext context) {\n        return AlertDialog(\n          title: Text('SMS \xec\xbd\x94\xeb\x93\x9c\xeb\xa5\xbc \xec\x9e\x85\xeb\xa0\xa5\xed\x95\xb4\xec\xa3\xbc\xec\x84\xb8\xec\x9a\x94'),\n          content: TextField(\n            onChanged: (value) {\n              this.smsCode = value;\n            },\n          ),\n          contentPadding: EdgeInsets.all(10),\n          actions: <Widget>[\n            FlatButton(\n              child: Text('Done'),\n              onPressed: () {\n                Navigator.of(context).pop(this.smsCode);\n              },\n            )\n          ],\n        );\n      },\n    );\n  }\n\n\n  SIGNIn() async {\n    String smsCode = await getSmsCode(context);\n    if (smsCode != null && !smsCode.isNotEmpty) {\n      print('User cancelled SMS dialog');\n      return;\n    }\n    final AuthCredential credential = PhoneAuthProvider.getCredential(\n      verificationId: verificationId,\n      smsCode: smsCode,\n    );\n    print('\xec\xa7\x84\xed\x96\x89\xec\xa4\x91');\n    FirebaseAuth _auth = FirebaseAuth.instance;\n    final FirebaseUser user = (await _auth.signInWithCredential(credential)).user;\n    final FirebaseUser currentUser = await _auth.currentUser();\n    assert(user.uid == currentUser.uid);\n    setState(() {\n      if (user != null) {\n        print('success!');\n      } else {\n        print('sign in failed');\n      }\n    });\n  }\n\n
Run Code Online (Sandbox Code Playgroud)\n\n

现在仅调用SIGNIn,它将首先获取短信代码,然后使用该短信代码登录。希望能帮助到你。

\n