got this error on flutter "The argument type 'AuthCredential' can't be assigned to the parameter type 'PhoneAuthCredential'."

Viewed 439

I have been making a mobile verification page using firebase authentication and I got this error "The argument type 'AuthCredential' can't be assigned to the parameter type 'PhoneAuthCredential'.". Please help me I have been trying to find a solution for this for the past 2 days.

import 'package:firebase_auth/firebase_auth.dart';
import "package:flutter/material.dart";
'Home_Screen.dart';

enum MobileVerificationState {
  SHOW_MOBILE_FORM_STATE,
  SHOW_OTP_FORM_STATE,
}

class LoginScreen extends StatefulWidget {
  @override
  _LoginScreenState createState() => _LoginScreenState();
}

class _LoginScreenState extends State<LoginScreen> {
  MobileVerificationState currentState =
      MobileVerificationState.SHOW_MOBILE_FORM_STATE;

  final phoneController = TextEditingController();
  final otpController = TextEditingController();

  FirebaseAuth _auth = FirebaseAuth.instance;

  late String verificationId;

  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){
        Navigator.push(context, MaterialPageRoute(builder: (context)=> HomeScreen()));
      }

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

          ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text("Login failed")));
    }
  }

  getMobileFormWidget(context) {
    return Column(
      children: [
        Spacer(),
        TextField(
          controller: phoneController,
          decoration: InputDecoration(
            hintText: "Phone Number",
          ),
        ),
        SizedBox(
          height: 16,
        ),
        FlatButton(
          onPressed: () async {
            setState(() {
              showLoading = true;
            });

            await _auth.verifyPhoneNumber(
              phoneNumber: phoneController.text,
              verificationCompleted: (phoneAuthCredential) async {
                setState(() {
                  showLoading = false;
                });
                //signInWithPhoneAuthCredential(phoneAuthCredential);
              },
              verificationFailed: (verificationFailed) async {
                setState(() {
                  showLoading = false;
                });
                ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text("Login             failed")));
          },
              codeSent: (verificationId, resendingToken) async {
                setState(() {
                  showLoading = false;
                  currentState = MobileVerificationState.SHOW_OTP_FORM_STATE;
                  this.verificationId = verificationId;
                });
              },
              codeAutoRetrievalTimeout: (verificationId) async {},
            );
          },
          child: Text("SEND"),
          color: Colors.blue,
          textColor: Colors.white,
        ),
        Spacer(),
      ],
    );
  }

  getOtpFormWidget(context) {
    return Column(
      children: [
        Spacer(),
        TextField(
          controller: otpController,
          decoration: InputDecoration(
            hintText: "Enter OTP",
          ),
        ),
        SizedBox(
          height: 16,
        ),
        TextButton(
          onPressed: () async {
            AuthCredential phoneAuthCredential =
                PhoneAuthProvider.credential(
                    verificationId: verificationId, smsCode: otpController.text);

            signInWithPhoneAuthCredential(**phoneAuthCredential**);
          },
          child: Text("VERIFY"),
          
        ),
        Spacer(),
      ],
    );
  }
                                                                                                              
                                                             ‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎

‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎

2 Answers

It seems that you are trying to pass AuthCredential as PhoneAuthCredential in the following code:

getOtpFormWidget(context) {
    ...
        TextButton(
          onPressed: () async {
            AuthCredential phoneAuthCredential =
                PhoneAuthProvider.credential(
                    verificationId: verificationId, smsCode: otpController.text);

            signInWithPhoneAuthCredential(phoneAuthCredential); // this line
          },
          child: Text("VERIFY"),
          
        ),
...
    );

Solution As described in the exception, You have typecasted PhoneAuthCredential to AuthCredential in this line:

AuthCredential phoneAuthCredential =
                PhoneAuthProvider.credential(
                    verificationId: verificationId, smsCode: otpController.text);

Meaning you have supercast it to AuthCredential explicitly, which dart can not downcast automatically to PhoneAuthCredential when you send it to signInWithPhoneAuthCredential method.

Solution

Just remove the type of phoneAuthCredential. Replace it with final or set it to it's original type PhoneAuthCredential.

Thank you, Harshvardhan joshi sir

  TextButton(
          onPressed: () async {
          final  AuthCredential  =
                PhoneAuthProvider.credential(
                   verificationId: verificationId, smsCode: otpController.text);

            signInWithPhoneAuthCredential();
          },
          child: Text("VERIFY"),
          
        ),

what argument should I add to "signInWithPhoneCredential"?

Related