'Flutter Firebase Login is not Successful

Firebase Authentication: email/Password was already set to Enabled. Realtime Database: users records exist. Email and Password: thoroughly checked and verified correct. below are the code for this issue;

final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
    
      void loginUser(BuildContext context) async {
        final User? firebaseUser = (await _firebaseAuth
                .signInWithEmailAndPassword(
          email: emailTextEditingController.text,
          password: passwordTextEditingController.text,
        )
                .catchError((errMsg) {
          displayTostMessage("Error: " + errMsg.toString(), context);
        }))
            .user;
        if (firebaseUser != null) {
          userRef
              .child(firebaseUser.uid)
              .once()
              .then((value) => (DataSnapshot snap) {
                    if (snap.value != null) {
                      Navigator.pushNamedAndRemoveUntil(
                          context, HomeScreen.idScreen, (route) => false);
                      displayTostMessage("You are logged in", context);
                    } else {
                      _firebaseAuth.signOut();
                      displayTostMessage(
                          "No info exist. Please creat an account", context);
                    }
                  });
        } else {
          displayTostMessage("Erro Accured, Can't be Signed-in", context);
        }
      }


Solution 1:[1]

Call this method to login, Hope it will help you.

Future<User?> fireLogin(String email, String password) async {
    FirebaseAuth _auth = FirebaseAuth.instance;

    try {
      final loggedInUser = await _auth.signInWithEmailAndPassword(
          email: email, password: password);

      AppSharedPreference().saveUserId(_auth.currentUser!.uid);
      print(_auth.currentUser!.uid);

      if (loggedInUser != null) {
        Navigator.pushReplacementNamed(context, '/bottomNavBar');
      }
    } catch (e) {
      print(e);
      return null;
    }
  }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vishal_VE