'The default 'List' constructor isn't available when null safety is enabled. Try using a list literal, 'List.filled' or 'List.generate'

Why List() constructor is not accessible after Dart's null safety?

// Compile time error: 'List' is deprecated and shouldn't be used.
// The default 'List' constructor isn't available when null safety is enabled. 
// Try using a list literal, 'List.filled' or 'List.generate'.
List<int> foo = List(); 

However, you can still do:

List<int> foo = []; // No error

So, what's the difference between the two? Either both of them should show the error or none of them.



Solution 1:[1]

Apart from what @lrn sir mentioned, you can also create a list using:

List<int> foo = List<int>.empty(growable: true); // []

Solution 2:[2]

This is because the the default List() element was deprecated how about you try using List.filled() element as shown below

void display() {
    var fixedList = new List<int>.filled(5, 0, growable: false);
    fixedList[0] = 0;
    fixedList[1] = 10;
    fixedList[2] = 20;
    fixedList[3] = 30;
    fixedList[4] = 40;
    print('Elements in the list are as follows: $fixedList');
  }
} 

While for the Growable Length List you can try doing as shown below:

void main() {
    var growableList = new List<int>.filled(0,0, growable:true);
    growableList = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90];
    print('The elements in the growable list include: $growableList');
  }

Solution 3:[3]

_items = List<DropdownMenuItem<String>>(); can be written in null safty as

_items = List<DropdownMenuItem<String>>.from(<DropdownMenuItem<String>>[]);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 iDecode
Solution 2 Lamech Desai
Solution 3 Jarvis098