firebase - 如何在 AlertDialo 中显示 Firebase 异常(错误)
问题描述
我想知道如何获取 firebase 异常并在 Flutter 的 alertDialog 中显示它们。谢谢!
这是在我的 auth_service 中登录的方法,我知道 cath(e) 在控制台中打印错误,但我想知道是否有任何方法可以编辑这些错误并在 alertDialog 中显示它们
这是我的 auth_servic.dart 文件的一个类中的方法
static void login(String email, String password) async {
try {
await _auth.signInWithEmailAndPassword(email: email, password: password);
} catch (e) {
print(e);
}
}
我的登录页面:
import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:jobly10/screens/google_signup.dart'; import 'package:jobly10/screens/passwordRecover.dart'; import 'package:jobly10/services/auth_service.dart'; import 'package:page_transition/page_transition.dart'; import '../checkifsignedupornot.dart'; import 'google_signup.dart'; import 'package:modal_progress_hud/modal_progress_hud.dart';
bool showSpinner = false;
class Login extends StatefulWidget { static final String id = 'login_screen'; @override _LoginState createState() =>
_LoginState(); }
class _LoginState extends State<Login> { bool showSpinner = false; final _formKey = GlobalKey<FormState>(); String _email, _password;
_submit() {
if (_formKey.currentState.validate()) {
_formKey.currentState.save();
// Logging in the user w/ Firebase
AuthService.login(_email, _password);
} }
@override Widget build(BuildContext context) {
return Scaffold(
body: ModalProgressHUD(
inAsyncCall: showSpinner,
child: Center(
child: Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Form(
key: _formKey,
child: Column(
children: <Widget>[
Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Center(
child: Container(
child: Center(
child: Text(
'Jobly',
style: TextStyle(
color: Colors.redAccent,
fontSize: 50,
fontFamily: 'Pacifico-Regular',
shadows: <Shadow>[
Shadow(
offset: Offset(2.0, 3.0),
blurRadius: 1.0,
color: Colors.black),
]),
),
),
),
),
],
),
),
SizedBox(
height: 10,
),
Container(
height: 50,
width: 320,
child: Center(
child: Text(
'No pierdas la opertunidad. Sigue active y actualizado',
style: TextStyle(
color: Colors.black26,
fontFamily: 'VarelaRound-Regular',
fontSize: 12.0),
),
),
),
Container(
height: 50,
width: 320,
child: TextFormField(
keyboardType: TextInputType.emailAddress,
cursorColor: Colors.black,
style: TextStyle(
fontFamily: 'Poppins-Medium',
fontSize: 15.0,
color: Colors.black),
decoration: InputDecoration(
contentPadding: const EdgeInsets.all(8.0),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(5.0),
),
labelText: 'Correo',
),
validator: (input) => input.trim().isEmpty
? 'Ingrese un correo valido'
: null,
onSaved: (input) => _email = input,
),
),
SizedBox(height: 10.0),
Container(
height: 50,
width: 320,
child: TextFormField(
obscureText: true,
cursorColor: Colors.black,
style: TextStyle(
fontFamily: 'Poppins-Medium',
fontSize: 15.0,
color: Colors.black),
decoration: InputDecoration(
contentPadding: const EdgeInsets.all(8.0),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(5.0),
),
labelText: 'Contraseña',
),
validator: (input) => input.trim().isEmpty
? 'Ingrese una contraseña valida'
: null,
onSaved: (input) => _password = input,
),
),
SizedBox(height: 10.0),
Container(
height: 50,
width: 320,
decoration: BoxDecoration(
gradient: LinearGradient(colors: [
Colors.black87,
Colors.black87,
]),
borderRadius: BorderRadius.circular(6.0),
),
margin: EdgeInsets.symmetric(
vertical: 0.0, horizontal: 0.0),
child: Material(
color: Colors.transparent,
child: InkWell(
highlightColor: Colors.deepOrange,
splashColor: Colors.black87,
onTap: () {
setState(() {
showSpinner = true;
});
_submit();
},
child: Center(
child: Text(
"Ingresar",
style: TextStyle(
color: Colors.white,
backgroundColor: null,
fontFamily: "CM Sans Serif",
fontSize: 12,
letterSpacing: 1.0),
),
),
),
),
),
SizedBox(height: 20.0),
Container(
height: 15,
width: 320,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Expanded(
child: Container(
height: 1,
color: Colors.black26,
),
),
SizedBox(
width: 10.0,
),
Center(
child: Text(
'or',
style: TextStyle(
color: Colors.black26, fontSize: 13.0),
),
),
SizedBox(
width: 10.0,
),
Expanded(
child: Container(
height: 1,
color: Colors.black26,
),
),
],
),
),
SizedBox(height: 15.0),
InkWell(
child: Text(
"Registrase.",
style: TextStyle(
color: Colors.black,
fontFamily: "CM Sans Serif",
fontSize: 13,
letterSpacing: 1.0),
),
onTap: () {
Navigator.push(
context,
PageTransition(
type: PageTransitionType.upToDown,
child:
CheckingSignup(), //will pass the route to the CheckIfSignedupOrNot Class
duration: Duration(milliseconds: 200),
),
);
},
),
InkWell(
child: Text(
"Perdi mi contraseña.",
),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) {
return PasswordRecover();
},
),
);
},
),
InkWell(
child: Text(
"Google Sign In",
),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) {
return GoogleSigninSignUp();
},
),
);
},
),
InkWell(
child: Text(
"Ingresar con numero de telefono",
),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) {
return CheckingPhoneLogin();
},
),
);
},
),
],
),
),
],
),
),
),
),
);
} }
解决方案
欢迎来到stackoverflow :)
这是我用来处理所有错误的 AlertDialog:
void error(String error, BuildContext context,
{Color backgroundColor = Colors.white,
TextAlign textAlign = TextAlign.start,
double fontSize = 20,
FontWeight fontWeight = FontWeight.bold,
Color textColor = Colors.red}) {
AlertDialog errorAlertDialog = new AlertDialog(
elevation: 0,
backgroundColor: backgroundColor,
content: SelectableText(
error,
textAlign: textAlign,
style: TextStyle(
color: textColor, fontSize: fontSize, fontWeight: fontWeight),
));
showDialog(context: context, child: errorAlertDialog);
}
您需要给他您的 error.toString() (在您的情况下: e.toString() )和实际上下文。
推荐阅读
- javascript - 如何在同一页面上多次添加firebase recapcha?
- java - 使用 Java Streams API 创建具有父子结构的嵌套对象集合?
- mongodb - How to make subtract operation using result of projected Field in MongoDb
- python-3.x - 如何从当前日期减去 5 天
- node.js - Node.js + Oracledb 4.2 + executeMany + 错误:NJS-011:遇到绑定值和类型不匹配
- git - 如何告诉'git'不要跟踪链接本身,而是跟踪它的内容
- sublimetext3 - 如何为 Sublime Text 3 中的特定语法完全禁用 Emmet?
- jquery - 事件是火,如果我添加类
- python - Python:是否可以将所有导入变量从单独的文件转换为类变量?
- php - 多维数组在php中转换为字符串