I'm creating login authentication, login has been successful but to enter the dashboard it doesn't go directly to the redirect but has to restart the android emulator, below is the code I made
import 'package:flutter/material.dart';
import 'package:get/get_core/get_core.dart';
import 'package:get/get_instance/src/extension_instance.dart';
import 'package:surveys/presentation/home/authServices.dart';
import 'package:firebase_auth/firebase_auth.dart';
class LoginScreen extends StatefulWidget {
const LoginScreen({ Key? key }) : super(key: key);
@override
_LoginScreenState createState() => _LoginScreenState();
}
class _LoginScreenState extends State<LoginScreen> {
final emailC = TextEditingController();
final passwordC = TextEditingController();
void login(String email, String password) async {
FirebaseAuth _auth = FirebaseAuth.instance;
User? user;
try {
UserCredential userCredential = await _auth.signInWithEmailAndPassword(
email: email,
password: password);
user = userCredential.user;
} on FirebaseAuthException catch (e) {
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.');
}
}
}
here I call login
onPressed: () {
//login(context);
login(emailC.text, passwordC.text);
},
CodePudding user response:
I don't see any Navigator
to Dashboard Screen
In your case:
try {
UserCredential userCredential = await _auth.signInWithEmailAndPassword(
email: email,
password: password);
user = userCredential.user;
if(user != null){
setState(() {
_success = true;
user = user.email;
});
}
else{_success = false;}
} on FirebaseAuthException catch (e) {
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.');
}
Then trigger _success
If(_success )
{
Navigator.push(context,MaterialPageRoute(builder: (context) => const Dashboard()),);
}
CodePudding user response:
Try This Code
Future<String> loginwithemailandpassword(String email,String password) async {
FirebaseAuth firebaseauth = FirebaseAuth.instance;
try {
UserCredential result = await firebaseauth.signInWithEmailAndPassword(
email: email,
password: password,
);
if(result != null)
{
setState(() {
user = result.user;
});
return "Success";
}
else
{
return "Unsuccess";
}
} on FirebaseAuthException catch (error) {
//print(error.code);
if (error.code == "invalid-email") {
return "Invalid Email";
} else if (error.code == "user-disabled") {
return "User Disabled";
} else if (error.code == "user-not-found") {
return "User not found";
} else if (error.code == "wrong-password") {
return "Wrong Password";
} else {
return "Unknown Error";
}
} on SocketException catch (e) {
print("Socket Exception Error $e");
return "Network Error";
} on TimeoutException catch (e) {
print("Timeout Error $e");
return "Timeout Error";
} catch (e) {
print("UnExpected Error::$e");
return "UnExpected Error";
}
}
Then OnPressed Write Following Code:
onPressed: () async{
//login(context);
String message = await loginwithemailandpassword(emailC.text, passwordC.text);
if(message == "Success")
{
//Do Navigate Like Following
Navigator.push(context,MaterialPageRoute(builder: (context) => const Dashboard()),);
}
else
{
print(message);
}
},