flutter - 如何在带有Firebase的Flutter中使用电话号码进行身份验证?

标签 flutter dart firebase-authentication

我是新来的人,
并使用Firebase通过电话号码进行身份验证,但我无法进行身份验证。
我浏览了一下,但没有任何令人满意的解决方案。

我从StackOverflow实现了一些代码,但是发生了异常'Unhandled Exception:type'(FirebaseUser)=> Null'不是类型强制转换类型'(AuthCredential)=> void'的子类型,并且无法捕获该异常。

signIn()async{
    AuthCredential credential= PhoneAuthProvider.getCredential(
      verificationId: verificationId,
      smsCode: smsCode
    );
    await firebaseAuth.signInWithCredential(credential).then((user){
       Navigator.of(context).pushReplacementNamed('/homepage');
         print('signed in with phone number successful: user -> $user');
    }).catchError((e){
       print(e);
    });
  }



它将向您发送OTP到给定的电话号码。
Future<void> verifyPhone()async{
    final PhoneCodeAutoRetrievalTimeout autoRetrieval=(String verId){
      this.verificationId=verId;
    };

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

    final PhoneVerificationCompleted verificationCompleted = (FirebaseUser user) {
     print('verified');
    } as PhoneVerificationCompleted;



    final PhoneVerificationFailed verfifailed=(AuthException exception){
      print("${exception.message}");
    };

    await firebaseAuth.verifyPhoneNumber(
     phoneNumber: this.phoneNo,
     codeAutoRetrievalTimeout: autoRetrieval,
     codeSent: smsCodeSent,
     timeout: const Duration(seconds: 10),
     verificationCompleted: verificationCompleted,
     verificationFailed: verfifailed
    );
  }

pubspec.yaml


name: babilok
description: A new Flutter project.

version: 1.0.0+1

environment:
  sdk: ">=2.1.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter


  cupertino_icons: ^0.1.2
  page_transition:
  firebase_auth:

dev_dependencies:
  flutter_test:
    sdk: flutter


  uses-material-design: true

  # To add assets to your application, add an assets section, like this:
  assets:
    - assets/login_wheel.png
    - assets/login_wheel1.png
    - assets/forward_arrow.png
    - assets/google_ads.png
    - assets/wheel.png
    - assets/red_cross.png
    - assets/welcome_logo.png
    - assets/money_bag.png
    - assets/user.png
    - assets/money_bag.png
    - assets/up_gift_wheel.png
    - assets/cross.png
    - assets/icons/facebook_logo1.png
    - assets/icons/twitter_logo1.png
    - assets/icons/instagram_logo1.png

最佳答案

我已经从stackOverflow here找到了解决方案,

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

关于flutter - 如何在带有Firebase的Flutter中使用电话号码进行身份验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57373903/

相关文章:

firebase - 用户使用Firebase注册时如何保存名称?

firebase - 在 Android 设备上发布时未发送 OTP

flutter :如何在按下后退按钮时不退出应用程序

flutter - Dart中解密AES/CBC/PKCS5Padding加密

firebase - 从Firebase存储获取图像

Dart 聚合物自定义元素动态链接

java - 任务成功后附加参数未存储在 Firebase 数据库中?

python - 使用 python 从 firebase 注销

dart - Flutter firebase_admob 仅显示一次

flutter - 此小部件的所有子项都必须在 Reorderable Listview 中有一个键