I have the following code snippet in Dart:
void main(List<String> args){
String? test1 = null;
String test2 = test1.toString();
print(test1);
print(test2);
}
I expected the second print()
statement to output an empty string because test2
is declared as a non-nullable String
. However, it outputs “null” instead. Can someone explain why this is happening?
Thank you.
Because null
is still an instance. Sole instance of class Null
and it has toString()
method implemented. See here for details: Null class