The method 'getCredential' isn't defined for the type 'PhoneAuthProvider'

Viewed 1741

As far as I can see, there are numerous deprecated methods in flutter firebase authentication.

here is my code;

            FlatButton(
              child: Text("Confirm"),
              textColor: Colors.white,
              color: Colors.blue,
              onPressed: () async {
                FirebaseAuth _auth = FirebaseAuth.instance; // I didn't use it here. I did for you to see
                final code = _codeController.text.trim();
                final AuthCredential credential = PhoneAuthProvider.getCredential(
                        verificationId: verificationId, smsCode: code);

                UserCredential result =
                    await _auth.signInWithCredential(credential);

                User user = result.user;

                if (user != null) {
                  Navigator.push(
                      context,
                      MaterialPageRoute(
                          builder: (context) =>
                              HomeScreen(user: user)));
                } else {
                  print("Error");
                }
              },
            )

I try to do phone auth but getCredential has error. I looked at firebase documentation. it says that PhoneAuthProvider deprecated and I should use PhoneAuthCredential with getCredential. I used it but it didn't work. I saw that there are various question about this error on stackoverflow but I can't find workaround yet

1 Answers

This is how I implemented it in my app on firebase_auth: ^0.18.0+1:

  @override
  Future<void> signInWithSmsCode(String smsCode) async {
    final AuthCredential authCredential = PhoneAuthProvider.credential(
      smsCode: smsCode,
      verificationId: _verificationCode,
    );
    try {
      await auth.signInWithCredential(authCredential);
    } on PlatformException catch (e) {
      throw _firebaseErrorFactory.getException(e.code);
    } on FirebaseAuthException {
      throw _firebaseErrorFactory
          .getException('ERROR_INVALID_VERIFICATION_CODE');
    }
  }
Related