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.
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);
}
},
),
CodePudding user response:
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);
}
},
),
now, looking at that line:
final newUser =
await _auth.createUserWithEmailAndPassword(email: email, password: password);
create a new user as expected, then it will return the result of that request which is a UserCredentail
that contains some specific user's information such as email, access token, uid..., so if you created the user account and then save the UserCredentail
without using it, it will show you a warning (not really an error):
The value of the local variable isn’t used In Dart
now if we used that variable like this as example:
print(newUser);
the warning will disappear because it's used now.
if you're not planning to use that variable in your actual project, then you can just call the createUserWithEmailAndPassword
like this:
await _auth.createUserWithEmailAndPassword(email: email, password: password);
Without saving it in a variable, it will create a new account for the user as expected.
Hope this helps.