Please Using flutter could somone help create me a list of exceptions for the login page which I would provide below such as user not found exception, wrong password exception and generic auth exception if possible. I have tried but its not working. If you look at the code I tried using a future which did not work . Please help me
import ‘package:firebase_auth/firebase_auth.dart’;
import ‘package:flutter/material.dart’;
import ‘package:thehunt/constants/routes.dart’;
import ‘package:thehunt/views/forgot_password_view.dart’;
class LoginView extends StatefulWidget {
final VoidCallback showRegisterView;
const LoginView({super.key, required this.showRegisterView});
@override
State
}
class _LoginViewState extends State
//text controllers
late final TextEditingController _email;
late final TextEditingController _password;
Future logIn() async {
await FirebaseAuth.instance.signInWithEmailAndPassword(
email: _email.text.trim(),
password: _password.text.trim(),
);
}
@override
void initState() {
_email = TextEditingController();
_password = TextEditingController();
super.initState();
}
@override
void dispose() {
_email.dispose();
_password.dispose();
super.dispose();
}
Future loginCatch() async {
try {
await FirebaseAuth.instance.signInWithEmailAndPassword(
email: _email.text.trim(),
password: _password.text.trim(),
);
} on FirebaseAuthException catch (e) {
if (e.code == ‘user-not-found’) {
showDialog(
context: context,
builder: (context) {
return const AlertDialog(
content: Text(‘User not found’),
);
},
);
} else if (e.code == ‘wrong-password’) {
print(‘wrong password’);
}
;
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey[300],
body: SafeArea(
child: Center(
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
‘Welcome to The Hunt’,
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 30,
),
),
SizedBox(height: 36),
//email field
Padding(
padding: const EdgeInsets.symmetric(horizontal: 25.0),
child: Container(
decoration: BoxDecoration(
color: Colors.grey[200],
border: Border.all(color: Colors.white),
borderRadius: BorderRadius.circular(12),
),
child: Padding(
padding: const EdgeInsets.only(left: 20.0),
child: TextField(
decoration: const InputDecoration(
border: InputBorder.none,
hintText: ‘Enter your email here’,
),
controller: _email,
enableSuggestions: false,
autocorrect: false,
keyboardType: TextInputType.emailAddress,
),
),
),
),
const SizedBox(height: 10),
//password field
Padding(
padding: const EdgeInsets.symmetric(horizontal: 25.0),
child: Container(
decoration: BoxDecoration(
color: Colors.grey[200],
border: Border.all(color: Colors.white),
borderRadius: BorderRadius.circular(12),
),
child: Padding(
padding: const EdgeInsets.only(left: 20.0),
child: TextField(
decoration: const InputDecoration(
border: InputBorder.none,
hintText: ‘Enter your password ‘,
),
controller: _password,
obscureText: true,
enableSuggestions: false,
autocorrect: false,
),
),
),
),
const SizedBox(height: 10),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 25.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) {
return ForgotPasswordView();
},
),
);
},
child: Text(
‘Forgot your password?’,
style: TextStyle(
color: Colors.blue,
fontWeight: FontWeight.bold,
),
),
),
],
),
),
//login button
const SizedBox(height: 10),
Padding(
padding: const EdgeInsets.symmetric(
horizontal: 25.0,
),
child: GestureDetector(
onTap: logIn,
child: Container(
padding: EdgeInsets.all(20),
decoration: BoxDecoration(
color: Colors.deepPurple,
borderRadius: BorderRadius.circular(12),
),
child: const Center(
child: Text(
‘Login’,
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 18,
),
)),
),
),
),
const SizedBox(height: 36),
// register now
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
GestureDetector(
//onTap: widget.showRegisterPage,
child: Text(
‘Not registered yet?’,
style: TextStyle(
fontWeight: FontWeight.bold,
),
),
),
GestureDetector(
onTap: widget.showRegisterView,
child: Text(
‘ Register now’,
style: TextStyle(
color: Colors.blue,
fontWeight: FontWeight.bold,
),
),
),
],
)
// TextButton(
// onPressed: () {
// Navigator.of(context).pushNamedAndRemoveUntil(
// registerRoute,
// (route) => false,
// );
// },
// child: const Text(‘Not registered yet? Register here’),
// )
],
),
),
),
),
);
}
}