Why is the type checking in Dart such strange?

137 Views Asked by At

Given code

List<int> rawList = [0, 1, 2];
BuiltList<int> list = rawList.map((n) => n * 2);

compiles successfully but fails at runtime with the error

type 'MappedListIterable<int, int>' is not a subtype of type 'BuiltList'

So why wouldn't it crash on compile stage? Please explain, after C# and Kotlin I can't understand that.

1

There are 1 best solutions below

4
Randal Schwartz On

.map returns an Iterable<int>, which doesn't fit in your List<int>. You need to convert the Iterable to a List with .toList().