首页 > 解决方案 > 错误:“PhoneAuthCredential”不是类型

问题描述

对于以下获取 OTP 的代码,我在 firebase 上遇到错误。几个小时前它对我来说很好,但现在我收到了以下几组错误。我认为由于主题或主题中提到的问题,最终会出现以下错误。

Running Gradle task 'assembleDebug'...
lib/OTP/OTPController.dart:38:33: Error: 'PhoneAuthCredential' isn't a type.
        verificationCompleted: (PhoneAuthCredential credential) async {
                                ^^^^^^^^^^^^^^^^^^^
lib/OTP/OTPController.dart:39:39: Error: The method 'signInWithCredential' isn't defined for the class 'FirebaseAuth'.
 - 'FirebaseAuth' is from 'package:firebase_auth/firebase_auth.dart' ('../../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/firebase_auth-0.6.2+1/lib/firebase_auth.dart').
Try correcting the name to the name of an existing method, or defining a method named 'signInWithCredential'.
          await FirebaseAuth.instance.signInWithCredential(credential).then((value) {
                                      ^^^^^^^^^^^^^^^^^^^^
lib/OTP/OTPController.dart:57:17: Error: The argument type 'Null Function(String, int)' can't be assigned to the parameter type 'void Function(String, [int])'.
      codeSent: (String vID, int resendToken)
                ^
lib/OTP/OTPController.dart:119:51: Error: The method 'signInWithCredential' isn't defined for the class 'FirebaseAuth'.
 - 'FirebaseAuth' is from 'package:firebase_auth/firebase_auth.dart' ('../../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/firebase_auth-0.6.2+1/lib/firebase_auth.dart').
Try correcting the name to the name of an existing method, or defining a method named 'signInWithCredential'.
                      await FirebaseAuth.instance.signInWithCredential(PhoneAuthProvider.
                                                  ^^^^^^^^^^^^^^^^^^^^
lib/OTP/OTPController.dart:119:72: Error: The getter 'PhoneAuthProvider' isn't defined for the class '_OTPControllerState'.
 - '_OTPControllerState' is from 'package:jiwdopani/OTP/OTPController.dart' ('lib/OTP/OTPController.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'PhoneAuthProvider'.
                      await FirebaseAuth.instance.signInWithCredential(PhoneAuthProvider.
                                                                       ^^^^^^^^^^^^^^^^^
    class OTPController extends StatefulWidget {
      final String phone;
      final String codeDigits;
    
      OTPController({this.phone, this.codeDigits});
    
      @override
      _OTPControllerState createState() => _OTPControllerState();
    }
    
    class _OTPControllerState extends State<OTPController> {
      final GlobalKey<ScaffoldState> _scafoldkey = GlobalKey<ScaffoldState>();
      final TextEditingController _pinOTPCodeController = TextEditingController();
      final FocusNode _pinOTPFocus = FocusNode();
      String verificationcode;
      final BoxDecoration pinOTPCodeDecoration = BoxDecoration(
          color: Colors.indigo[900],
          borderRadius: BorderRadius.circular(10.0),
          border: Border.all(
            color: Colors.grey,
          ));
      @override
      void initState(){
        super.initState();
        verifyPhoneNumber();
    
      }
      verifyPhoneNumber() async{
    
        await FirebaseAuth.instance.verifyPhoneNumber(
            phoneNumber: "${widget.codeDigits+widget.phone}",
            verificationCompleted: (PhoneAuthCredential credential) async {
              await FirebaseAuth.instance.signInWithCredential(credential).then((value) {
                if (value != null) {
                  Navigator.of(context).push(
                      MaterialPageRoute(builder: (c) => HomeScreen()));
                }
              }
              );
            },
            verificationFailed: (FirebaseError){
    
              ScaffoldMessenger.of(context).showSnackBar(
                SnackBar(
        content: Text('Invalid OTP'),
        duration: Duration(seconds: 3)
                )
              );
        },
    
          codeSent: (String vID, int resendToken)
          {
              setState(() {
                verificationcode=vID;
              });
          },
          codeAutoRetrievalTimeout:(String vID){
              setState(() {
                verificationcode=vID;
              });
        },
          timeout: Duration(seconds:60),
        );
    
            }
    
    
    
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
            key: _scafoldkey,
            appBar: AppBar(
              title: Text('OTP Verification'),
            ),
            body: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Image.asset("assets/otp.png"),
                ),
                Container(
                    margin: EdgeInsets.only(top: 20),
                    child: Center(
                        child: GestureDetector(
                            onTap: () {
    
                            },
                            child: Text(
                              'Verifying your number :${widget.codeDigits}-${widget.phone}',
                              style: TextStyle(
                                fontWeight: FontWeight.bold,
                                fontSize: 16,
                              ),
                            )))),
                Padding(
                    padding: EdgeInsets.all(40),
                    child: PinPut(
                      fieldsCount: 6,
                      textStyle: TextStyle(fontSize: 25.0, color: Colors.white),
                      eachFieldWidth: 40.0,
                      eachFieldHeight: 55.0,
                      focusNode: _pinOTPFocus,
                      controller: _pinOTPCodeController,
                      submittedFieldDecoration: pinOTPCodeDecoration,
                      selectedFieldDecoration: pinOTPCodeDecoration,
                      followingFieldDecoration: pinOTPCodeDecoration,
                      pinAnimationType: PinAnimationType.rotation,
                      onSubmit: (pin) async {
                        try {
                          await FirebaseAuth.instance.signInWithCredential(PhoneAuthProvider.
                              credential(
                                  verificationId: verificationcode, smsCode: pin))
                              .then((value) {
                            if (value != null) {
                              Navigator.of(context).push(
                                  MaterialPageRoute(builder: (c) => HomeScreen()));
                            }
                          });
                        } catch (e) {
                          FocusScope.of(context).unfocus();
                          ScaffoldMessenger.of(context).showSnackBar(SnackBar(
                              content: Text('Invalid OTP'),
                              duration: Duration(seconds: 3)));
                        }
                      },
                    ))
              ],
            ));
      }
    }

标签: androidfirebaseflutterdartfirebase-authentication

解决方案


推荐阅读