我目前正在重设密码页面。我正在使用 firebase auth,我正在尝试使用“try catch”来捕获错误。当我以错误的格式输入电子邮件或将其留空时,我想显示一些警告对话框。但是,当我写错电子邮件或将其留空时,我的应用程序崩溃并给出此错误消息:
发生异常。 PlatformException(PlatformException(无效电子邮件,电子邮件地址格式错误。,{code:无效电子邮件,消息:电子邮件地址格式错误。,nativeErrorMessage:电子邮件地址格式错误。,nativeErrorCode:17008,additionalData:{ }},空))
完整代码:
// ignore_for_file: use_build_context_synchronously
import 'dart:io';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:petinonedemo/main.dart';
class ForgotPasswordPage extends StatefulWidget {
const ForgotPasswordPage({super.key});
@override
State<ForgotPasswordPage> createState() => _ForgotPasswordPageState();
}
class _ForgotPasswordPageState extends State<ForgotPasswordPage> {
final TextEditingController _emailController = TextEditingController();
final TextEditingController _phoneNumberController = TextEditingController();
@override
void dispose() {
_emailController.dispose();
_phoneNumberController.dispose();
super.dispose();
}
void _showIOSAlert(BuildContext context, Text myContent) {
showCupertinoModalPopup<void>(
context: context,
builder: (BuildContext context) => CupertinoAlertDialog(
content: myContent,
actions: <CupertinoDialogAction>[
CupertinoDialogAction(
isDefaultAction: true,
onPressed: () {
Navigator.pop(context);
},
child: const Text('Geri dön'),
),
],
),
);
}
void _showAndroidAlert(BuildContext context, Text myContent) {
showDialog(
context: context,
barrierDismissible: true,
builder: (BuildContext context) {
return AlertDialog(
content: myContent,
//buttons?
actions: <Widget>[
TextButton(
child: const Text("Geri dön"),
onPressed: () {
Navigator.of(context).pop();
}, //closes popup
),
],
);
},
);
}
Future passwordReset() async {
String resetMail = _emailController.text.toString();
try {
await FirebaseAuth.instance
.sendPasswordResetEmail(email: _emailController.text.trim());
if (Platform.isIOS) {
_showIOSAlert(
context,
Text('$resetMail adresine şifre sıfırlama linki gönderildi'),
);
} else {
_showAndroidAlert(
context,
Text('$resetMail adresine şifre sıfırlama linki gönderildi'),
);
}
} on FirebaseAuthException catch (e) {
if (Platform.isIOS) {
return _showIOSAlert(
context,
Text(
e.message.toString(),
),
);
} else {
return _showAndroidAlert(
context,
Text(
e.message.toString(),
),
);
}
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Şifremi Unuttum"),
elevation: 0,
),
body: SafeArea(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
height: 70,
padding: const EdgeInsets.symmetric(horizontal: 25),
child: TextField(
controller: _emailController,
cursorColor: Colors.white,
keyboardType: TextInputType.emailAddress,
style: const TextStyle(color: Colors.white),
decoration: InputDecoration(
floatingLabelBehavior: FloatingLabelBehavior.never,
hintText: "E-posta",
hintStyle: const TextStyle(color: Colors.white),
labelStyle: const TextStyle(color: Colors.white),
filled: true,
fillColor: applicationPurple,
border: OutlineInputBorder(
borderSide: BorderSide.none,
borderRadius: BorderRadius.circular(20)))),
),
Container(
decoration: BoxDecoration(
color: applicationOrange,
borderRadius: BorderRadius.circular(20)),
height: 50,
padding: const EdgeInsets.symmetric(horizontal: 25.0),
child: TextButton(
onPressed: () {
passwordReset();
},
child: const Text(
"Şifremi Sıfırla",
style: TextStyle(
color: Colors.white, fontWeight: FontWeight.bold),
),
),
),
],
),
),
),
);
}
}
你得到这个异常是因为你只捕获 firebase 异常:
试试这个:
// ignore_for_file: use_build_context_synchronously
import 'dart:io';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:petinonedemo/main.dart';
class ForgotPasswordPage extends StatefulWidget {
const ForgotPasswordPage({super.key});
@override
State<ForgotPasswordPage> createState() => _ForgotPasswordPageState();
}
class _ForgotPasswordPageState extends State<ForgotPasswordPage> {
final TextEditingController _emailController = TextEditingController();
final TextEditingController _phoneNumberController = TextEditingController();
@override
void dispose() {
_emailController.dispose();
_phoneNumberController.dispose();
super.dispose();
}
void _showIOSAlert(BuildContext context, Text myContent) {
showCupertinoModalPopup<void>(
context: context,
builder: (BuildContext context) => CupertinoAlertDialog(
content: myContent,
actions: <CupertinoDialogAction>[
CupertinoDialogAction(
isDefaultAction: true,
onPressed: () {
Navigator.pop(context);
},
child: const Text('Geri dön'),
),
],
),
);
}
void _showAndroidAlert(BuildContext context, Text myContent) {
showDialog(
context: context,
barrierDismissible: true,
builder: (BuildContext context) {
return AlertDialog(
content: myContent,
//buttons?
actions: <Widget>[
TextButton(
child: const Text("Geri dön"),
onPressed: () {
Navigator.of(context).pop();
}, //closes popup
),
],
);
},
);
}
Future passwordReset() async {
String resetMail = _emailController.text.toString();
try {
await FirebaseAuth.instance
.sendPasswordResetEmail(email: _emailController.text.trim());
if (Platform.isIOS) {
_showIOSAlert(
context,
Text('$resetMail adresine şifre sıfırlama linki gönderildi'),
);
} else {
_showAndroidAlert(
context,
Text('$resetMail adresine şifre sıfırlama linki gönderildi'),
);
}
} on FirebaseAuthException catch (e) {
if (Platform.isIOS) {
return _showIOSAlert(
context,
Text(
e.message.toString(),
),
);
} else {
return _showAndroidAlert(
context,
Text(
e.message.toString(),
),
);
}
} catch (_){
//-------------------------
print('Generic Auth Exception')
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Şifremi Unuttum"),
elevation: 0,
),
body: SafeArea(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
height: 70,
padding: const EdgeInsets.symmetric(horizontal: 25),
child: TextField(
controller: _emailController,
cursorColor: Colors.white,
keyboardType: TextInputType.emailAddress,
style: const TextStyle(color: Colors.white),
decoration: InputDecoration(
floatingLabelBehavior: FloatingLabelBehavior.never,
hintText: "E-posta",
hintStyle: const TextStyle(color: Colors.white),
labelStyle: const TextStyle(color: Colors.white),
filled: true,
fillColor: applicationPurple,
border: OutlineInputBorder(
borderSide: BorderSide.none,
borderRadius: BorderRadius.circular(20)))),
),
Container(
decoration: BoxDecoration(
color: applicationOrange,
borderRadius: BorderRadius.circular(20)),
height: 50,
padding: const EdgeInsets.symmetric(horizontal: 25.0),
child: TextButton(
onPressed: () {
passwordReset();
},
child: const Text(
"Şifremi Sıfırla",
style: TextStyle(
color: Colors.white, fontWeight: FontWeight.bold),
),
),
),
],
),
),
),
);
}
}