gpt4 book ai didi

firebase - 在UI Firebase Auth Flutter中显示异常错误

转载 作者:行者123 更新时间:2023-12-03 04:53:12 25 4
gpt4 key购买 nike

首先,我是Flutter的新手,我读了很多线程,但一无所获。

我的问题是什么?

我有两个页面 auth.dart login.dart 。在 auth.dart 页面中,我有一个名为 AuthService 的类,该类由以下代码组成(到目前为止):

class AuthService {

final FirebaseAuth _auth = FirebaseAuth.instance;

Future<bool> signInWithPhone(String phone, BuildContext context) async {
....
verificationFailed: (AuthException exception) {
// i can see the exception in debug console using print
return exception;
},
....
}

}

login.dart 中,我有一个有状态的小部件,看起来像这样,在其中引用了上面的类,并在调用 signInWithPhone 方法:
class LoginScreen extends StatefulWidget {
@override
_LoginScreenState createState() => _LoginScreenState();
}

class _LoginScreenState extends State<LoginScreen> {

final AuthService _auth = AuthService();
....
....
Container(
width: double.infinity,
child: FlatButton(
child: Text("LOGIN"),
textColor: Colors.white,
padding: EdgeInsets.all(16),
onPressed: () {
// here i am calling the method signInWithPhone of above class

final phone = _phoneController.text.trim();
_auth.signInWithPhone(phone, context);

}
)
)
....
....
}

我想要什么?

我想显示在登录按钮的onPressed事件中收到的异常错误消息,以便我可以使用该错误消息显示警报对话框。

我尝试过什么?

1) print(_auth.signInWithPhone(phone, context));但是它返回空值;

2)
_auth.signInWithPhone(phone, context).then((value){
// alert/text/print
print(value);
}

这也返回null

谁能帮我解决这个问题,因为如果手机号码不正确或OTP不正确,我需要向用户显示错误消息。

最佳答案

好吧,首先,您需要侦听异常并等待它,可以在期望的情况下返回false,并根据情况进行相应的处理。

您可以返回包含结果和这样的消息的 map

{
'message': message,
'success' : true or false depending on the state of the request
}


所以

AuthService.dart
 verificationFailed: (AuthException exception) {
// i can see the exception in debug console using print
//return exception;
return {
'message': exception,
'success': false
}; // means verification failed;
},



LoginScreen.dart

void showAlert(message) {
showDialog(context: context, builder: (context) {
return AlertDialog(
title: message,
);
});
}

onPressed: async () {
// here i am calling the method signInWithPhone of above class

final phone = _phoneController.text.trim();
final result = await _auth.signInWithPhone(phone, context);


if(!result['suceess']) {
this.showAlert(result['message']);
}
}

关于firebase - 在UI Firebase Auth Flutter中显示异常错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61049317/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com