Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unhandled Exception: Null check operator used on a null value with signInUsingEmailPassword

Hello I have old code working before null safety, after migration and firebase upgrade, I can’t anymore login to an account.
I have take care to register before login, all my firebase setup is OK but

When I try to print("${user?.email} or print("${user?.displayName} or print("${user?.emailVerified} It return null

Here is my code

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

First I have a FuturBuilder who call _initializeFirebase()

FutureBuilder(
          future: _initializeFirebase(),

   Future<FirebaseApp> _initializeFirebase() async {
        FirebaseApp firebaseApp = await Firebase.initializeApp();
         _currentUser = FirebaseAuth.instance.currentUser!.emailVerified;
    
        User? user = FirebaseAuth.instance.currentUser;
   
        return firebaseApp;
      }

After I have a raised button who call signInUsingEmailPassword

                   new RaisedButton(
                        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)),
                        elevation: 5,
                        highlightElevation: 10,
                        color: Color(0xffff9a7b),
                        splashColor: Colors.white,
                        child :

                        new  Text("Charger mes données",textAlign: TextAlign.center,  style: TextStyle(color: Colors.white, fontWeight: FontWeight.w500, fontSize:15)),

                        padding: const EdgeInsets.all (15.0),
                        onPressed: () async{

                          _focusEmail.unfocus();
                          _focusPassword.unfocus();

                          if(_currentUser==false){
                            if (_formKey.currentState!
                                .validate()) {
                              setState(() {
                                _isProcessing = true;
                              });

                              User? user = await signInUsingEmailPassword(
                                email: _emailTextController.text.replaceAll(' ', ''),
                                password:
                                _passwordTextController.text.replaceAll(' ', ''),
                              );

                              setState(() {
                                _isProcessing = false;
                              });
                             
                            }
                          }

                        },
                      ),

After I have error , print("$user user"); return Unhandled Exception: Null check operator used on a null value

   Future<User?> signInUsingEmailPassword({

      String? email,
     String? password,
     
  }) async {
    FirebaseAuth auth = FirebaseAuth.instance;
    User? user;
    print("$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.');
        showDialog(
            context: context,
            builder: (context) {
              return AlertDialog(
                title: Text('Erreur'),
                content: Text("Email incorrect"),

              );
            });
      } else if (e.code == 'wrong-password') {
        print('Erreur de mot de passe.');
        showDialog(
            context: context,
            builder: (context) {
              return AlertDialog(
                title: Text('Erreur'),
                content: Text("Mot de passe incorrect"),

              );
            });
      }
    }

    return user;
  }

>Solution :

Instead of using !try using ? By adding an exclamation you mention that its not null but value is unknown. By adding a ? You mean that it maybe or maynot be null

print("${user?.email});

It also means that the user is null. Maybe there is some error in the firebase initialisation

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading