Reputation: 4091
In the following code , bar is a List
but right side is a WhereIterable
, As dart is a type safe language I have expected to have an error in Build time, but I have Uncaught Error: TypeError: Instance of 'WhereIterable<int>': type 'WhereIterable<int>' is not a subtype of type 'List<int>'
error in Run time
void main() {
final List<int> foo = [1, 2, 3];
final List<int> bar = foo.where((e) => e > 1);
}
I know that adding .toList()
fix error, My question is : Why we have no error in Compile/Build time?
Upvotes: 0
Views: 103
Reputation: 3768
This is because dart implicitly casts Iterable to List. This is default behavior. In order to disable this one should turn of implicit casts in analyzer settings like this:
analyzer:
strong-mode:
implicit-casts: false
More information can be found here: https://dart.dev/guides/language/analysis-options#enabling-additional-type-checks and here: https://dart.dev/guides/language/type-system
Upvotes: 1