The following is my event which get called when tapped on send code button after entering phone number
on<sendOtpEvent>((event,emit)async{
await _auth.verifyPhoneNumber(
phoneNumber:event.number,
verificationCompleted:(PhoneAuthCredential credentials){},
verificationFailed: (FirebaseAuthException e) {
emit(ErrorState(error: e.message.toString()));
},
codeSent: (String verificationId, int? forceResendingToken) async{
},
codeAutoRetrievalTimeout: (String verificationId) {
});
});
This is Login Screen Page on which the send code button will call the send otp event
TextEditingController _phoneController=TextEditingController();
final _formKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: BlocBuilder<whatsAppBloc,whatsAppStates>(
builder: (context,state){
if(state is verifyOTPState){
Navigator.pushReplacement(context,MaterialPageRoute(builder:(context)=>VerifyOTP(verificationID: state.verificationID, CurrentUserPhoneNumber:_phoneController.text)));
}
if(state is ErrorState)
{
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content:Text(state.error.toString())));
}
return SingleChildScrollView(
child: SizedBox(
height: MediaQuery.sizeOf(context).height,
width: MediaQuery.sizeOf(context).width,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Image.asset(
"assets/Images/whatsapp.png",
scale: 4,
),
RichText(
textAlign: TextAlign.center,
text: const TextSpan(children: [
TextSpan(
style: TextStyle(
height: 2.5,
fontFamily: "Helvetica",
fontWeight: FontWeight.w600,
color: Color.fromRGBO(89, 176, 114, 1),
fontSize: 33),
text: "WhatsAppn"),
TextSpan(
style: TextStyle(
height: 1.5,
fontFamily: "Helvetica",
color: Colors.black,
fontSize: 14,
fontWeight: FontWeight.normal),
text: "Enter your Phone Number nto continuen"),
])),
Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expanded(
flex: 1,
child: Container(
height: 55,
alignment: Alignment.center,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
border: Border.all(color: Colors.grey)
),
child: Text("+91",style: TextStyle(fontSize: 16,fontWeight:FontWeight.normal,fontFamily: "Helvetica"),),
),
),
SizedBox(width: 5,),
Expanded(
flex: 7,
child: Form(
key: _formKey,
child: TextFormField(
controller: _phoneController,
validator: (value) {
if (value == null || value.length < 10) {
return "Please enter a valid phone number";
} else {
return null;
}
},
textAlign: TextAlign.start,
cursorColor: Colors.black,
style: const TextStyle(
fontWeight: FontWeight.normal,
fontFamily: "Helvetica",
fontSize: 16),
decoration: InputDecoration(
focusedBorder: OutlineInputBorder(borderRadius: BorderRadius.circular(10),borderSide: BorderSide(color: Colors.green)),
border: OutlineInputBorder(borderRadius: BorderRadius.circular(10),borderSide: BorderSide(color: Colors.black)),
),
keyboardType: TextInputType.phone,
),
),
),
],
),
),
ElevatedButton(
onPressed: () async{
if (_formKey.currentState!.validate() == true){
_formKey.currentState?.save();
BlocProvider.of<whatsAppBloc>(context).add(sendOtpEvent(context:context,number:"+91${_phoneController.text}"));
}
},
style: ElevatedButton.styleFrom(
elevation: 0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12)),
backgroundColor: Color.fromRGBO(32, 152, 34, 1),
foregroundColor: Colors.white),
child: const Text(
"Send Code",
style: TextStyle(
fontFamily: "Helvetica",
fontSize: 15,
fontWeight: FontWeight.bold),
))
],
),
),
);
},
),
);
}
The following is my event for calling firebase phone auth
class sendOtpEvent extends whatsAppEvents{
BuildContext context;
String number;
sendOtpEvent({required this.number,required this.context});
}
Following are my states which will get called by my events
class ErrorState extends whatsAppStates{
String error;
ErrorState({required this.error});
}
class verifyOTPState extends whatsAppStates{
String verificationID;
verifyOTPState({required this.verificationID});
}
I have tried with async with the codeSent body but it does not worked please provide solution to this error
New contributor
Dnyanesh Kulkarni is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.