I want to send to me verification code for my phone number or any phone number with firebase and need to add to my code country picker when i install latest version show to me error
verfication page
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'VerificationCode.dart';
class VerificationPage extends StatefulWidget {
static const String screenroute = "VerificationPage";
const VerificationPage({super.key});
@override
State<VerificationPage> createState() => _VerificationPageState();
}
class _VerificationPageState extends State<VerificationPage> {
final TextEditingController phoneNumberController = TextEditingController();
final FirebaseAuth _auth = FirebaseAuth.instance;
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
backgroundColor: Colors.black,
elevation: 0,
title: Text(
"Enter Your Phone Number",
style: TextStyle(color: Colors.white),
),
centerTitle: true,
actions: [
IconButton(
onPressed: () {},
icon: Icon(Icons.more_vert, color: Colors.white))
],
),
body: Column(
children: [
Padding(
padding: EdgeInsets.all(8.0),
child: RichText(
textAlign: TextAlign.center,
text: TextSpan(
text: 'ProGuard will need to verify your phone number.',
style: TextStyle(
color: Colors.grey,
height: 1.5,
),
children: [
TextSpan(
text: "What's my number?",
style: TextStyle(color: Colors.blue))
],
),
),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 50),
child: TextField(
controller: phoneNumberController,
keyboardType: TextInputType.phone,
decoration: InputDecoration(
labelText: 'Enter Your Phone Number',
prefixIcon: Icon(Icons.phone),
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(10)),
),
),
),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: () async {
await _verifyPhoneNumber(phoneNumberController.text);
},
child: Text("NEXT"),
),
],
),
);
}
@override
void dispose() {
phoneNumberController.dispose();
super.dispose();
}
Future<void> _verifyPhoneNumber(String phoneNumber) async {
await _auth.verifyPhoneNumber(
phoneNumber: '+2001032774725',
verificationCompleted: (PhoneAuthCredential credential) async {
// Automatically signs the user in.
await _auth.signInWithCredential(credential);
Navigator.pushNamed(context, 'UserProfile');
},
verificationFailed: (FirebaseAuthException e) {
if (e.code == 'invalid-phone-number') {
print('The provided phone number is not valid.');
} else {
print('Verification failed. Code: ${e.code}. Message: ${e.message}');
}
},
codeSent: (String verificationId, int? resendToken) async {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => VerificationCode(
verificationId: verificationId,
),
),
);
},
codeAutoRetrievalTimeout: (String verificationId) {},
timeout: const Duration(seconds: 60),
);
}
}
verification code page means otp code
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
class VerificationCode extends StatefulWidget {
final String verificationId;
const VerificationCode({super.key, required this.verificationId});
@override
_VerificationCodeState createState() => _VerificationCodeState();
}
class _VerificationCodeState extends State<VerificationCode> {
final TextEditingController codeController = TextEditingController();
final FirebaseAuth _auth = FirebaseAuth.instance;
String errorMessage = '';
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
backgroundColor: Colors.black,
elevation: 0,
title: Text(
"Enter Verification Code",
style: TextStyle(color: Colors.white),
),
centerTitle: true,
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
TextField(
controller: codeController,
keyboardType: TextInputType.number,
decoration: InputDecoration(
labelText: 'Enter Verification Code',
prefixIcon: Icon(Icons.verified),
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(10)),
),
errorText: errorMessage.isEmpty ? null : errorMessage,
),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: () async {
await _verifyCode();
},
child: Text("VERIFY"),
),
],
),
),
);
}
Future<void> _verifyCode() async {
String smsCode = codeController.text.trim();
if (smsCode.isEmpty) {
setState(() {
errorMessage = 'Please enter the verification code';
});
return;
}
try {
PhoneAuthCredential credential = PhoneAuthProvider.credential(
verificationId: widget.verificationId,
smsCode: smsCode,
);
await _auth.signInWithCredential(credential);
Navigator.pushNamed(context, 'UserProfile');
} catch (e) {
if (e is FirebaseAuthException && e.code == 'invalid-verification-code') {
setState(() {
errorMessage = 'Invalid verification code. Please try again.';
});
} else {
setState(() {
errorMessage = 'An error occurred. Please try again.';
});
}
}
}
@override
void dispose() {
codeController.dispose();
super.dispose();
}
}
I
when i entered my number with country code not send any message i want to help me in this issue want to send to me verification code for my phone number or any phone number with firebase and need to add to my code country picker when i install latest version show to me error