Open Yegair opened 5 hours ago
Summary: The issue occurs when using pattern matching inside a collection literal with a nullable type. In release builds, the pattern matching guard appears to be removed during compilation, leading to null values being included in the collection, resulting in a runtime error when accessing those values. This issue is specific to release builds and does not occur in development builds.
When using an
if(someValue case final Type typedValue)
guard within collection literals, there can be runtime errors that I think are caused by an incorrect compilation. They only happen in release builds (dart compile exe
) and don't happen in development builds (dart run
).I encountered this issue with Dart
3.5.3
, but I did not test if it is present in other Dart versions also.Reproduction
When running the code above with
dart run
, it will correctly print out 3 random values, such as[10, 32, 29]
.However, when compiling it to an executable and then running it, it will throw an error like
And the stack-trace pointing to the line with the statement
I assume the line
somehow gets erased by the compiler, which then causes the
null
value to end up in thenonNullValues
list.Also some observations I have made:
String? get valueOrNull
toint? get valueOrNull
and then pattern matching on... case final int ...
does not seem to cause the same problemto
also causes the error to vanish