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

Receiving warning message for null operator

I declared _auth and called it in the register button, so it takes me to another screen
but I am receiving a warning that says;

"The operand can’t be null, so the condition is always true."

Please help me solve this problem, I really do not understand the problem at all.

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

  final _auth = FirebaseAuth.instance; //This is where I declared the auth variable

  late String email;
  late String password;
 
            RoundedButton(
              color: Colors.blueAccent,
              title: 'Register',
              onPress: () async {
                try {
                  final newUser =
                      await _auth.createUserWithEmailAndPassword(email: email, password: password);

                  if (newUser != null) { //This is where the warning comes from
                    if (!mounted) return;
                    Navigator.pushNamed(context, ChatScreen.id);
                  }
                } catch (e) {
                  print(e);
                }
              },
            ),
          

>Solution :

the await _auth.createUserWithEmailAndPassword(email: email, password: password) will return an UserCredential object, which it will never be null, so making an if else condition over it doesn’t do anything really since the newUser != null will always equals to true.

the piece of code you wrote is equivalent to this:

 final _auth = FirebaseAuth.instance; //This is where I declared the auth variable

late String email;
late String password;

        RoundedButton(
          color: Colors.blueAccent,
          title: 'Register',
          onPress: () async {
            try {
              final newUser =
                  await _auth.createUserWithEmailAndPassword(email: email, password: password);

                if (!mounted) return;
                Navigator.pushNamed(context, ChatScreen.id);
            
            } catch (e) {
              print(e);
            }
          },
        ),
      

I’m assuming that you want to catch exceptions/erros that might be thrown from this auth operation like an invalid-email, weak-password, email-already-in use, for that you need to catch with the special Exception class of Firebase Auth, the FirebaseAuthException, so your code should be like this now:

  late String email;
  late String password;

        RoundedButton(
          color: Colors.blueAccent,
          title: 'Register',
          onPress: () async {
            try {
              final newUser =
                  await _auth.createUserWithEmailAndPassword(email: email, password: password);

                if (!mounted) return;
                Navigator.pushNamed(context, ChatScreen.id);
            
            } on FirebaseAuthException catch (e) {
              print(e.code);
              print(e.message);
            }
          },
        ),
      

Hope this helps.

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