Dart compiler does not understand that the variable can not be null when I use it inside an if (x != null)
statement. It still requires to use conditional ?
or null check !
operators to access one of the variable's fields or methods.
Here is an example:
String? string;
void test() {
if (string != null) {
print(string.length);
}
}
This produces a compile-time error and says that
The property 'length' can't be unconditionally accessed because the receiver can be 'null'. Try making the access conditional (using '?.') or adding a null check to the target ('!').
However, the receiver actually can't be null since it's wrapped with if (string != null)
block. Accessing the field with string?.length
or string!.length
works fine but it can be confusing when I need to use different fields or methods of the variable.
String? string;
void test() {
if (string != null) {
print(string.length);
print(string.isNotEmpty);
print(string.trim());
print(string.contains('x'));
}
}
All of these statements raise the same error. I also tried putting assert(string != null);
but the compiler still does not understand that the string is not null.
To give a more sophisticated example;
User? user;
@override
Widget build(BuildContext context) {
if (user == null) {
return Text('No user available');
} else {
return buildUserDetails();
}
}
Widget buildUserDetails() {
return Column(
children: [
Text(user.name),
Text(user.email),
],
);
}
This is still a problem for the compiler.
final user = this.user;
. I am currently using this approach and I believe it is reasonable for accessing its properties likeif (user != null) { user.name; }
. – Trefoil