1

error: The default 'List' is not available when null safety is enabled

how do i create an empty array with fixed length in Dart?

main(List<String> args) {
  List<int> lista_int_fissa = new List<int>(3);//
  lista_int_fissa[1] = 200;
  print(lista_int_fissa);
}
  • 1
    Does this answer your question? [How to replace deprecated List](https://stackoverflow.com/questions/66862780/how-to-replace-deprecated-list) – jamesdlin Apr 04 '21 at 22:16
  • Does this answer your question? [The default 'List' constructor isn't available when null safety is enabled. Try using a list literal, 'List.filled' or 'List.generate'](https://stackoverflow.com/questions/63451506/the-default-list-constructor-isnt-available-when-null-safety-is-enabled-try) – iDecode Aug 26 '22 at 21:42

2 Answers2

2

With Null safety enabled you have to declare your list the following way:

var lista_int_fissa = new List<int?>(3);
Antonin GAVREL
  • 9,682
  • 8
  • 54
  • 81
2

Yes, since null-safety in 2.12 the List constructors have changed to avoid null values. You could use List.filled(3, 0) to create a list of 3 zeroes.

List<int> lista_int_fissa = List.filled(3, 0);

The documentation about the List constructor indicates that it's now deprecated and suggests using List.filled instead. You could also create use a literal to create a list

List<int> lista_int_fissa = [0, 0, 0];
dumazy
  • 13,857
  • 12
  • 66
  • 113