微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Flutter:如何为 firebase auth 插件实现 recaptcha

如何解决Flutter:如何为 firebase auth 插件实现 recaptcha

我想实现 firebase auth 方法,但是当我通过 recaptcha 时它要我 recaptcha 我面临这个问题

E/Flutter (19828): [ERROR:Flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: NoSuchMethodError: The method 'delegateFor' was called on null.
E/Flutter (19828): Receiver: null
E/Flutter (19828): Tried calling: delegateFor(container: "recptcha",onError: Closure: (FirebaseAuthException) => Null,onExpired: Closure: () => void,onSuccess: Closure: () => void,size: Instance of 'RecaptchaVerifierSize',theme: Instance of 'RecaptchaVerifierTheme')

这是我的代码

FirebaseAuth.instance.signInWithPhoneNumber(
  phoneNumber,RecaptchaVerifier(
    container: 'recptcha',onError: (e) {
      print(e);
    },onExpired: () => print("Expired"),onSuccess: () => print("Success"),size: RecaptchaVerifierSize.compact,theme: RecaptchaVerifierTheme.light,),);

解决方法

var recaptchaVerifier = RecaptchaVerifier(
        container: null,size: RecaptchaVerifierSize.compact,theme: RecaptchaVerifierTheme.dark,onSuccess: () async {
          print('reCAPTCHA Completed!');
          setState(() {
            currentInput = "otp";
            _isLoading = false;
          });
        },onError: (FirebaseAuthException error) async {
          print("error");
          print(error);
          setState(() {
            _isLoading = false;
          });
          _scaffoldKey.currentState
              .showSnackBar(SnackBar(content: Text(error.message)));
        },onExpired: () async {
          print('reCAPTCHA Expired!');
          setState(() {
            _isLoading = false;
          });
        },);
```
await _auth.signInWithPhoneNumber(phoneController.text,recaptchaVerifier).then((confirmationResult) {
        // SMS sent. Prompt user to type the code from the message,then sign the
        // user in with confirmationResult.confirm(code).
        setState(() {
          verificationId = confirmationResult.verificationId;
        });
      }).catchError((error) {
        print(error);
      });
```

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。