Anonymous
Я хочу отправить мне код подтверждения для моего номера телефона или любого номера телефона
Сообщение
Anonymous » 21 май 2024, 15:39
Я хочу отправить мне код подтверждения для моего номера телефона или любого номера телефона с Firebase, и мне нужно добавить его в средство выбора страны, когда я устанавливаю последнюю версию, покажите мне ошибку
страница проверки
Код: Выделить всё
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 createState() => _VerificationPageState();
}
class _VerificationPageState extends State {
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 _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),
);
}
}
Кодовая страница проверки означает OTP-код
Код: Выделить всё
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 {
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 _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();
}
}
когда я ввел свой номер с кодом страны, не отправляйте никаких сообщений, я хочу помочь мне в этом вопросе, хочу отправить мне код подтверждения для моего номера телефона или любого номера телефона с Firebase, и мне нужно чтобы добавить в мой код выбора страны, когда я устанавливаю последнюю версию, покажите мне ошибку
Подробнее здесь:
https://stackoverflow.com/questions/785 ... one-number
1716295199
Anonymous
Я хочу отправить мне код подтверждения для моего номера телефона или любого номера телефона с Firebase, и мне нужно добавить его в средство выбора страны, когда я устанавливаю последнюю версию, покажите мне ошибку страница проверки [code]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 createState() => _VerificationPageState(); } class _VerificationPageState extends State { 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 _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), ); } } [/code] Кодовая страница проверки означает 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 { 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 _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(); } } [/code] [code]I [/code] когда я ввел свой номер с кодом страны, не отправляйте никаких сообщений, я хочу помочь мне в этом вопросе, хочу отправить мне код подтверждения для моего номера телефона или любого номера телефона с Firebase, и мне нужно чтобы добавить в мой код выбора страны, когда я устанавливаю последнюю версию, покажите мне ошибку Подробнее здесь: [url]https://stackoverflow.com/questions/78511942/i-want-to-send-to-me-verification-code-for-my-phone-number-or-any-phone-number[/url]