Flutter Firebase电话身份验证未发送短信代码。直接调用PhoneCodeAutoRetrievalTimeout方法。这是我的代码

时间:2020-09-22 14:13:02

标签: flutter firebase-authentication

我只有复制粘贴的示例代码,才能开始使用Firebase Phone Authentication。但是,firebase不会将SMS代码发送给真实数字和测试数字。我都测试了。您能帮忙找出问题所在吗?

这是Flutter Doctor的输出

Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 1.20.4, on Microsoft Windows [Version 10.0.19041.508], locale en-IN)
 
[√] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
[√] Android Studio (version 4.0)
[√] VS Code (version 1.49.1)
[√] Connected device (1 available)

•找不到问题!


    import 'package:firebase_auth/firebase_auth.dart';
    
    class AuthService {
      String _phoneNumber;
      String _verificationId;
      String _smsCode;
      String _message;
    
      final FirebaseAuth _auth = FirebaseAuth.instance;
    
      String get phoneNumber1 => _phoneNumber;
      String get verificationId => _verificationId;
      String get smsCode => _smsCode;
      String get message => _message;
    
      Future<void> verifyPhoneNumber(String phoneNumber) async {
        print(phoneNumber);
        PhoneVerificationCompleted verificationCompleted =
            (PhoneAuthCredential phoneAuthCredential) async {
          print('User Auto Logged In');
          // UserCredential _userCredential =
          //     await _auth.signInWithCredential(phoneAuthCredential);
        };
    
        PhoneVerificationFailed verificationFailed =
            (FirebaseAuthException authException) async {
          print('Auth Exception Occured');
          // throw Exception(authException.message);
          // _message =
          //     'Phone number verification failed. Code: ${authException.code}. Message: ${authException.message}';
        };
    
        PhoneCodeSent codeSent =
            (String verificationId, [int forceResendingToken]) async {
          print('SMS Sent');
          _verificationId = verificationId;
        };
    
        PhoneCodeAutoRetrievalTimeout codeAutoRetrievalTimeout =
            (String verificationId) async {
          print('SMS Auto Retrieval Timeout');
          _verificationId = verificationId;
        };
    
        await _auth.verifyPhoneNumber(
            phoneNumber: phoneNumber,
            timeout: const Duration(seconds: 5),
            verificationCompleted: verificationCompleted,
            verificationFailed: verificationFailed,
            codeSent: codeSent,
            codeAutoRetrievalTimeout: codeAutoRetrievalTimeout);
      }
    
      // Example code of how to sign in with phone.
      void signInWithPhoneNumber(String smsCode) async {
        final AuthCredential credential = PhoneAuthProvider.credential(
          verificationId: _verificationId,
          smsCode: smsCode,
        );
        final User user = (await _auth.signInWithCredential(credential)).user;
      }
    }

输出:


    I/BiChannelGoogleApi( 2258): [FirebaseAuth: ] getGoogleApiForMethod() returned Gms: com.google.firebase.auth.api.internal.zzaq@57888fb
    Reloaded 4 of 704 libraries in 1,013ms.
    I/flutter ( 2258): SMS Auto Retrieval Timeout

请帮助我解决这个问题。

1 个答案:

答案 0 :(得分:0)

问题出在firebase本身。那天没有发送任何短信。现在,相同的代码可以正常工作。