Search code examples
flutterexceptiondartcompile-time-type-checking

why dart didn't throw compile-time mistype exception instead. of run-time?


Consider this sample code:

void main() {

  List<int> array = <int>[];

  for (int i = 0; i < 5; i++) {
    array.add(i);
  }

  Iterable<int> newList = array.where( (value) =>  value % 2 == 0 );

  printFunction(array);
  printFunction(newList);
}

void printFunction(List<int> list) {
 print(list); 
}

This code compile successfully but of course throw an exception in runtime, because it can't run the code for printFunction(newList) unless calling toList() before passing newList

Why compiler didn't throw an exception in this case???


Solution

  • This kind of check can be enabled via dart analyzer setting (analysis_options.yaml) as shown below:

    analyzer:
      strong-mode:
        implicit-casts: false