what is the difference exactly between
String id = folderInfo!.first.id; //this works
and
String id = folderInfo?.first.id; //this is an error
I know ?. returns null when the value object is null but what does the !. return?
?.
is known as Conditional member access
the leftmost operand can be null; example: foo?.bar selects property bar from expression foo unless foo is null (in which case the value of foo?.bar is null)
In your case, String id
means id
can not have null
value. But using ?.
can return null
that's why it is showing errors.
!.
is use If you know that an expression never evaluates to null
.
For example, a variable of type int? Might be an integer, or it might be null. If you know that an expression never evaluates to null but Dart disagrees, you can add !
to assert that it isn’t null (and to throw an exception if it is).
More and ref: important-concepts of null-safety and operators.