首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在颤动中自动读取firebase短消息中的OTP

问如何在颤动中自动读取firebase短消息中的OTP
EN

Stack Overflow用户
提问于 2021-09-12 15:01:29
回答 1查看 1.7K关注 0票数 0

我正在编写一个代码,要求在firebase中注册手机的OTP验证码。它的工作很好手动,但我想让代码或应用程序读取从firebase发送的短信代码,并自动检查它。例如,大多数应用程序在请求验证码时,手机收到验证码后,会自动将验证码写入验证码注册框中。

我找了很长一段时间,但对我来说没有用。

完整代码:

代码语言:javascript
复制
class _OtpScreenState extends State<OtpScreen> {
  String phoneNo;
  String smsOTP;
  String verificationId;
  String errorMessage = '';
  final FirebaseAuth _auth = FirebaseAuth.instance;
  bool showLoading = false;
  void signInWithPhoneAuthCredential(PhoneAuthCredential phoneAuthCredential) async {
    setState(() {
      showLoading = true;
    });

    try {
      final authCredential = await _auth.signInWithCredential(phoneAuthCredential);

      setState(() {
        showLoading = false;
      });

      if (authCredential?.user != null) {


      }
    } on FirebaseAuthException catch (e) {
      setState(() {
        showLoading = false;
      });
      print(e.message);
    }
  }


  @override
  Widget build(BuildContext context) {

    return    MaterialApp(
      home: Scaffold(
        body: Center(
          child: SingleChildScrollView(
            child: Container(
              child: Column(

                children: [


                  Container(

                    padding: const EdgeInsets.all(16.0),

                    child: Column(

                      children: [

                        Row(
                          children: [
                            Expanded(
                              child: PinEntryTextField(

                                fields: 6,
                                onSubmit: (text) {
                                  smsOTP = text as String;
                                },
                              ),
                            ),
                          ],
                        ),

                        GestureDetector(
                          onTap: () {
                            verifyOtp();
                          },
                          child: Container(
                            margin: const EdgeInsets.all(8),
                            height: 45,
                            width: double.infinity,
                            decoration: BoxDecoration(
                              color:     CustomColors.Background,
                              borderRadius: BorderRadius.circular(0),
                            ),
                            alignment: Alignment.center,
                            child:  Text('Check'),

                          ),
                        ),
                      ],
                    ),
                  )
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }


  Future<void> generateOtp(String contact) async {
    final PhoneCodeSent smsOTPSent = (String verId, [int forceCodeResend]) {
      verificationId = verId;
    };
    try {
      await _auth.verifyPhoneNumber(
          phoneNumber: contact,
          codeAutoRetrievalTimeout: (String verId) {
            verificationId = verId;
          },
          codeSent: smsOTPSent,
          timeout: const Duration(seconds: 60),
          verificationCompleted: (AuthCredential phoneAuthCredential) {},
          verificationFailed: (exception) {
            print(exception);

          });
    } catch (e) {
      handleError(e as PlatformException);

    }
  }

  //Method for verify otp entered by user
  Future<void> verifyOtp() async {
    if (smsOTP == null || smsOTP == '') {
      print('please enter 6 digit otp');
      return;
    }
    try {

      final AuthCredential credential = PhoneAuthProvider.credential(
        verificationId: verificationId,
        smsCode: smsOTP,
      );
      signInWithPhoneAuthCredential(credential);

    } catch (e) {
      handleError(e as PlatformException);
    } }

  void handleError(PlatformException error) {
    switch (error.code) {
      case 'ERROR_INVALID_VERIFICATION_CODE':
        FocusScope.of(context).requestFocus(FocusNode());
        setState(() {
          errorMessage = 'Invalid Code';
        });
        print('error');
        break;
      default:
        print('error');
        break;
    }
  }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-09-12 15:08:04

IOS默认提供短信自动填充功能。

对于安卓系统,你可以使用this package。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69152621

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档