【问题标题】:How to authenticate with phone number in flutter with firebase?如何在与firebase一起使用电话号码进行身份验证?
【发布时间】:2019-12-13 21:08:33
【问题描述】:

我是新来的颤振, 并使用 firebase 使用电话号码进行身份验证,但我无法进行身份验证。 我浏览了它,但找不到任何令人满意的解决方案。

我从 StackOverflow 实现了一些代码,但出现了异常“未处理的异常:类型 '(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

【问题讨论】:

  • 你试过this
  • 我更新了我的代码并得到了一个异常Unhandled Exception: type '(FirebaseUser) =&gt; Null' is not a subtype of type '(AuthCredential) =&gt; void' in type cast
  • 能否也请添加pubspec.yaml
  • 好的,我添加了 pubspec.yaml

标签: flutter dart firebase-authentication


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-02
    • 2020-11-08
    • 1970-01-01
    • 2018-03-23
    相关资源
    最近更新 更多