Harsh Kumar Yadav
Harsh Kumar Yadav

Reputation: 21

The operand can't be null, so the condition is always true

I am registering details in FireBase_Auth but since the type it take is not null and the value I want to take is nullable so that my if condition gets true . (if anyone has different solution to it kindly reply).

 final newUser = await _auth.createUserWithEmailAndPassword(
                        email: email, password: password);
                    if(newUser != null){
                      Navigator.pushNamed(context, ChatScreen.id);
                    }

The variable are declared as

 final _auth = FirebaseAuth.instance;
  String? email;
  String? password  ;

passing error

The argument type 'String?' can't be assigned to the parameter type 'String'.

The operand can't be null, so the condition is always true

Upvotes: 1

Views: 1978

Answers (1)

nvoigt
nvoigt

Reputation: 77354

I'm not sure what you want. createUserWithEmailAndPassword takes two strings, both of which need to be not null. If you have only nullable variables, you have to check whether they are non-null first before you pass them.

As for the return value, you don't need to check whether it's null, because it cannot be null. The return value of this method is Future<UserCredential> not Future<UserCredential?>. You can remove that line. If you want to account for errors, use try/catch blocks.

if(email != null && password != null) {
    try { 
        final newUser = await _auth.createUserWithEmailAndPassword(email, password);
        Navigator.pushNamed(context, ChatScreen.id);
    } catch(e) {
        // handle error here
    }
}

Upvotes: 1

Related Questions