如何处理Flatter中的PlatformException和FirebaseAuthException
我正在尝试验证Flitter中登录屏幕中的密码,以便在用户输入错误密码时显示错误。但它不起作用。当用户输入错误密码时,将返回PlatformException或FirebaseAuthException。我尝试了很多方法来尝试try-catch方法或catchError方法。这是我的auth.dart文件如何处理Flatter中的PlatformException和FirebaseAuthException,firebase,flutter,firebase-authentication,Firebase,Flutter,Firebase Authentication,我正在尝试验证Flitter中登录屏幕中的密码,以便在用户输入错误密码时显示错误。但它不起作用。当用户输入错误密码时,将返回PlatformException或FirebaseAuthException。我尝试了很多方法来尝试try-catch方法或catchError方法。这是我的auth.dart文件 import 'package:firebase_auth/firebase_auth.dart'; import 'package:flutter/cupertino.dart'; impo
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
final FirebaseAuth _auth = FirebaseAuth.instance;
Stream<User> get authStateChange => _auth.authStateChanges();
Future signIn(String email, String password) async {
UserCredential credential; /* */
try { /* */
credential = await _auth.signInWithEmailAndPassword( /* */
email: email, password: password /* */
); /* */
} on PlatformException catch(e) { /* <= This Is Not Working */
if (e.code == 'user-not-found') { /* */
print('No user found for that email.'); /* */
} else if (e.code == 'wrong-password') { /* */
print('Wrong password provided for that user.'); /* */
}
}
User user = credential.user;
return user;
}
Future createAccount(String email, String password) async {
try {
UserCredential credential = await _auth.createUserWithEmailAndPassword(
email: email, password: password
);
User user = credential.user;
return user;
} catch(e) {
print(e.toString());
}
}
Future resetPassword(String email) async {
try {
return await _auth.sendPasswordResetEmail(email: email);
} catch(e) {
print(e.toString());
}
}
Future signOut() async {
try {
return await _auth.signOut();
} catch(e) {
print(e.toString());
}
}
import'package:firebase_auth/firebase_auth.dart';
进口“包装:颤振/cupertino.dart”;
导入“包:flifter/services.dart”;
final FirebaseAuth _auth=FirebaseAuth.instance;
Stream get-authStateChange=>\u auth.authStateChanges();
未来登录(字符串电子邮件、字符串密码)异步{
用户凭证凭证;/**/
试试{/**/
credential=wait_auth.sign with Email and Password(/**/
电子邮件:电子邮件,密码:password/**/
); /* */
}在平台上异常捕获(e){/*我解决了问题。谢谢。如果你解决了问题,你应该告诉我们解决方案,而不仅仅是问题
Form(
key: formKey,
child: Column(
children: <Widget>[
TextFormField(
validator: (input) {
return RegExp(
r"^[a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+@[a-zA-Z0-9]+\.[a-zA-Z]+"
).hasMatch(input) ? null : 'Please Enter a Valid Email!';
},
controller: emailController,
decoration: InputDecoration(
suffixIcon: Icon(Icons.mail, size: 30,),
labelText: 'Email',
),
style: TextStyle(
fontSize: fieldFontSize
),
),
TextFormField(
controller: passwordController,
validator: (input) {
return input.length < 6 ? 'Your Password Can\'t Be Less Than 6 Characters.' : null;
},
decoration: InputDecoration(
suffixIcon: Icon(Icons.lock, size: 30,),
labelText: 'Password',
),
style: TextStyle(
fontSize: fieldFontSize
),
obscureText: true,
),
],
),
),
child: ElevatedButton(
onPressed: () {
if(formKey.currentState.validate()) {
setState(() {
isLoading = true;
});
signIn(emailController.text.trim(), passwordController.text).then((value) => {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Signed In Succesfully With ${emailController.text}!'),
),
),
Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => HomePage()))
}).catchError((e) {
String error = e.toString();
showDialog(context: context, builder: (context) => SimpleDialog(
title: Text(error),
));
});
}
},
child: Text('Sign In', style: TextStyle(fontSize: 28),),
),
),