首页 > 解决方案 > 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,
  ),
);

标签: firebaseflutterfirebase-authenticationrecaptchainvisible-recaptcha

解决方案


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);
      });
```

推荐阅读