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(),
],
);
}