Firebase ';CodeAutometricEvalTimeout!=空';:事实并非如此
我一周前开始学习颤振。我试图用Firebase_Auth软件包构建身份验证系统,但遇到了这个错误。目标是使用firebase phone auth和codeAutoRetrievalTimeout自动获取sms代码。 这是我的密码 LoginScreen.dartFirebase ';CodeAutometricEvalTimeout!=空';:事实并非如此,firebase,flutter,firebase-authentication,Firebase,Flutter,Firebase Authentication,我一周前开始学习颤振。我试图用Firebase_Auth软件包构建身份验证系统,但遇到了这个错误。目标是使用firebase phone auth和codeAutoRetrievalTimeout自动获取sms代码。 这是我的密码 LoginScreen.dart import 'package:flutter/material.dart'; import 'package:michot_2/screens/HomeScreen.dart'; import 'package:firebase_a
import 'package:flutter/material.dart';
import 'package:michot_2/screens/HomeScreen.dart';
import 'package:firebase_auth/firebase_auth.dart';
class LoginScreen extends StatelessWidget {
final _phoneController = TextEditingController();
final _codeController = TextEditingController();
Future<bool> loginUser(String phone, BuildContext context) async{
FirebaseAuth _auth = FirebaseAuth.instance;
_auth.verifyPhoneNumber(
phoneNumber: phone,
timeout: Duration(seconds: 60),
verificationCompleted: (AuthCredential credential) async{
Navigator.of(context).pop();
UserCredential result = await _auth.signInWithCredential(credential);
User user = result.user;
if(user != null){
Navigator.push(context, MaterialPageRoute(
builder: (context) => HomeScreen(user: user,)
));
}else{
print("Error");
}
//This callback would gets called when verification is done automatically
},
verificationFailed: (FirebaseAuthException exception){
print(exception);
},
codeSent: (String verificationId, [int forceResendingToken]){
showDialog(
context: context,
barrierDismissible: false,
builder: (context) {
return AlertDialog(
title: Text("Give the code?"),
content: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
TextField(
controller: _codeController,
),
],
),
actions: <Widget>[
FlatButton(
child: Text("Confirm"),
textColor: Colors.white,
color: Colors.blue,
onPressed: () async{
final code = _codeController.text.trim();
AuthCredential credential = PhoneAuthProvider.credential(verificationId: verificationId, smsCode: code);
UserCredential result = await _auth.signInWithCredential(credential);
User user = result.user;
if(user != null){
Navigator.push(context, MaterialPageRoute(
builder: (context) => HomeScreen(user: user,)
));
}else{
print("Error");
}
},
)
],
);
}
);
},
codeAutoRetrievalTimeout: null
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SingleChildScrollView(
child: Container(
padding: EdgeInsets.all(32),
child: Form(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("Login", style: TextStyle(color: Colors.lightBlue, fontSize: 36, fontWeight: FontWeight.w500),),
SizedBox(height: 16,),
TextFormField(
decoration: InputDecoration(
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(8)),
borderSide: BorderSide(color: Colors.grey[200])
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(8)),
borderSide: BorderSide(color: Colors.grey[300])
),
filled: true,
fillColor: Colors.grey[100],
hintText: "Mobile Number"
),
controller: _phoneController,
),
SizedBox(height: 16,),
Container(
width: double.infinity,
child: FlatButton(
child: Text("LOGIN"),
textColor: Colors.white,
padding: EdgeInsets.all(16),
onPressed: () {
final phone = _phoneController.text.trim();
loginUser(phone, context);
},
color: Colors.blue,
),
)
],
),
),
),
)
);
}
}
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
class HomeScreen extends StatelessWidget {
final User user;
HomeScreen({this.user});
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
padding: EdgeInsets.all(32),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text("You are Logged in succesfully", style: TextStyle(color: Colors.lightBlue, fontSize: 32),),
SizedBox(height: 16,),
Text("${user.phoneNumber}", style: TextStyle(color: Colors.grey, ),),
],
),
),
);
}
}
导入“包装:颤振/材料.省道”;
导入“包装:michot_2/screens/HomeScreen.dart”;
导入“包:firebase_auth/firebase_auth.dart”;
类LoginScreen扩展了无状态小部件{
final _phoneController=TextEditingController();
final _codeController=TextEditingController();
未来登录用户(字符串电话、BuildContext上下文)异步{
FirebaseAuth _auth=FirebaseAuth.instance;
_验证电话号码(
电话号码:电话,
超时:持续时间(秒数:60),
验证已完成:(AuthCredential凭据)异步{
Navigator.of(context.pop();
UserCredential result=wait _auth.signwithcredential(凭证);
User=result.User;
如果(用户!=null){
Navigator.push(上下文、MaterialPage路由)(
生成器:(上下文)=>主屏幕(用户:用户,)
));
}否则{
打印(“错误”);
}
//当验证自动完成时,将调用此回调
},
验证失败:(FirebaseAuthException异常){
打印(例外);
},
codeSent:(字符串验证ID,[int-forceResendingToken]){
显示对话框(
上下文:上下文,
禁止:错误,
生成器:(上下文){
返回警报对话框(
标题:文本(“给出代码?”),
内容:专栏(
mainAxisSize:mainAxisSize.min,
儿童:[
文本字段(
控制器:_codeController,
),
],
),
行动:[
扁平按钮(
子项:文本(“确认”),
textColor:Colors.white,
颜色:颜色,蓝色,
onPressed:()异步{
最终代码=_codeController.text.trim();
AuthCredential credential=PhoneAuthProvider.credential(验证ID:verificationId,smsCode:code);
UserCredential result=wait _auth.signwithcredential(凭证);
User=result.User;
如果(用户!=null){
Navigator.push(上下文、MaterialPage路由)(
生成器:(上下文)=>主屏幕(用户:用户,)
));
}否则{
打印(“错误”);
}
},
)
],
);
}
);
},
codeAutoRetrievalTimeout:null
);
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
正文:SingleChildScrollView(
子:容器(
填充:边缘设置。全部(32),
孩子:表格(
子:列(
crossAxisAlignment:crossAxisAlignment.start,
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
文本(“登录”,样式:TextStyle(颜色:Colors.lightBlue,fontSize:36,fontwweight:fontwweight.w500),
尺寸箱(高度:16,),
TextFormField(
装饰:输入装饰(
enabledBorder:OutlineInputBorder(
borderRadius:borderRadius.all(半径圆形(8)),
borderSide:borderSide(颜色:Colors.grey[200])
),
聚焦顺序:大纲输入边框(
borderRadius:borderRadius.all(半径圆形(8)),
borderSide:borderSide(颜色:Colors.grey[300])
),
是的,
fillColor:颜色。灰色[100],
hintText:“手机号码”
),
控制器:\ u电话控制器,
),
尺寸箱(高度:16,),
容器(
宽度:double.infinity,
孩子:扁平按钮(
子项:文本(“登录”),
textColor:Colors.white,
填充:边缘设置。全部(16),
已按下:(){
final phone=_phoneController.text.trim();
登录用户(电话、上下文);
},
颜色:颜色,蓝色,
),
)
],
),
),
),
)
);
}
}
主屏幕.省道
import 'package:flutter/material.dart';
import 'package:michot_2/screens/HomeScreen.dart';
import 'package:firebase_auth/firebase_auth.dart';
class LoginScreen extends StatelessWidget {
final _phoneController = TextEditingController();
final _codeController = TextEditingController();
Future<bool> loginUser(String phone, BuildContext context) async{
FirebaseAuth _auth = FirebaseAuth.instance;
_auth.verifyPhoneNumber(
phoneNumber: phone,
timeout: Duration(seconds: 60),
verificationCompleted: (AuthCredential credential) async{
Navigator.of(context).pop();
UserCredential result = await _auth.signInWithCredential(credential);
User user = result.user;
if(user != null){
Navigator.push(context, MaterialPageRoute(
builder: (context) => HomeScreen(user: user,)
));
}else{
print("Error");
}
//This callback would gets called when verification is done automatically
},
verificationFailed: (FirebaseAuthException exception){
print(exception);
},
codeSent: (String verificationId, [int forceResendingToken]){
showDialog(
context: context,
barrierDismissible: false,
builder: (context) {
return AlertDialog(
title: Text("Give the code?"),
content: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
TextField(
controller: _codeController,
),
],
),
actions: <Widget>[
FlatButton(
child: Text("Confirm"),
textColor: Colors.white,
color: Colors.blue,
onPressed: () async{
final code = _codeController.text.trim();
AuthCredential credential = PhoneAuthProvider.credential(verificationId: verificationId, smsCode: code);
UserCredential result = await _auth.signInWithCredential(credential);
User user = result.user;
if(user != null){
Navigator.push(context, MaterialPageRoute(
builder: (context) => HomeScreen(user: user,)
));
}else{
print("Error");
}
},
)
],
);
}
);
},
codeAutoRetrievalTimeout: null
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SingleChildScrollView(
child: Container(
padding: EdgeInsets.all(32),
child: Form(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("Login", style: TextStyle(color: Colors.lightBlue, fontSize: 36, fontWeight: FontWeight.w500),),
SizedBox(height: 16,),
TextFormField(
decoration: InputDecoration(
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(8)),
borderSide: BorderSide(color: Colors.grey[200])
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(8)),
borderSide: BorderSide(color: Colors.grey[300])
),
filled: true,
fillColor: Colors.grey[100],
hintText: "Mobile Number"
),
controller: _phoneController,
),
SizedBox(height: 16,),
Container(
width: double.infinity,
child: FlatButton(
child: Text("LOGIN"),
textColor: Colors.white,
padding: EdgeInsets.all(16),
onPressed: () {
final phone = _phoneController.text.trim();
loginUser(phone, context);
},
color: Colors.blue,
),
)
],
),
),
),
)
);
}
}
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
class HomeScreen extends StatelessWidget {
final User user;
HomeScreen({this.user});
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
padding: EdgeInsets.all(32),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text("You are Logged in succesfully", style: TextStyle(color: Colors.lightBlue, fontSize: 32),),
SizedBox(height: 16,),
Text("${user.phoneNumber}", style: TextStyle(color: Colors.grey, ),),
],
),
),
);
}
}
导入“包装:颤振/材料.省道”;
导入“包:firebase_auth/firebase_auth.dart”;
类主屏幕扩展无状态小部件{
最终用户;
主屏幕({this.user});
@凌驾
小部件构建(构建上下文){
返回脚手架(
主体:容器(
填充:边缘设置。全部(32),
子:列(
mainAxisAlignment:mainAxisAlignment.center,
crossAxisAlignment:crossAxisAlignment.start,
儿童:[
Text(“您已成功登录”,style:TextStyle(颜色:Colors.lightBlue,fontSize:32),
尺寸箱(高度:16,),
文本(${user.phoneNumber}),样式:TextStyle(颜色:Colors.grey,),),
],
),
),
);
}
}
主飞镖
import 'package:flutter/material.dart';
import 'package:michot_2/screens/LoginScreen.dart';
import 'package:firebase_core/firebase_core.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
class MyApp extends StatefulWidget {
// This widget is the root of your application.
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Michot Service',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: LoginScreen()
);
}
}
导入“包装:颤振/材料.省道”;
进口包装:micho