I built a login page using firebase. When the user clicks the login button, a CircularProgressIndicator is started and if the user gave the correct credentials everything works perfectly but if there is an error (e.g user not found OR wrong password) the user will be not forwarded to the next page but the CircularProgressIndicator will not stop showing.
The aim is, that if there is an error, I want to stop the CircularProgressIndicator and want to show an error message but I don't know how to stop the CircularProgressIndicator.
My code looks the following:
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
final navigatorKey = GlobalKey<NavigatorState>();
class LoginUser {
Future loginUser(userEmail, userPassword, context) async {
navigatorKey: navigatorKey;
showDialog(
barrierDismissible: false,
context: context,
builder: (context) => Center(child: CircularProgressIndicator())
);
try {
await FirebaseAuth.instance.signInWithEmailAndPassword(
email: userEmail,
password: userPassword
);
// Close Dialog when route changes
} on FirebaseAuthException catch (e) {
if (e.code == 'user-not-found') {
return "ec-unf";
} else if (e.code == 'wrong-password') {
return "ec-wp";
}
navigatorKey.currentState!.popUntil((route) => route.isFirst);
}
}
}
Does anybody know how to do this?
Thanks!
Chris
maybe you can add isLoading and finaly condition after try catch
isLoading = true
try {
//your code
print("done !");
isLoading
} catch (err) {
print("error");
isLoading = false
} finally {
print("Finish "); //error and success
isLoading = false
}