Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dart null vs Null

As far as a know, checking if a object is null or Null should be the same thing on Dart.

Something like:

print(bar == null);
print(bar is Null);

should print the same value. But, in a flutter app using firebase I got the following behaviour:

  print(snapshot.data['appearance']['details'].runtimeType); // Null
  print(snapshot.data['appearance']['details'].runtimeType == null); // false
  print(snapshot.data['appearance']['details'].runtimeType == Null); // true
  print(snapshot.data['appearance']['details'].runtimeType is Null); // false

the field value is null on firebase. Why is it behaving like this?

like image 987
Douglas Ramos Avatar asked Jan 30 '26 10:01

Douglas Ramos


1 Answers

null is a value. Null is a type.

.runtimeType returns a Type. It cannot return a null value (that would mean the object has no type). null.runtimeType returns Null; that is, the null object has a type named Null.

As for why .runtimeType is Null is false, see: What is the difference between 'is' and '==' in Dart?

like image 86
jamesdlin Avatar answered Feb 01 '26 01:02

jamesdlin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!