Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 303
iDecode Avatar asked Aug 17 '20 13:08

iDecode


People also ask

How to create an empty List in dart?

To create an empty list, use [] for a growable list or List. empty for a fixed length list (or where growability is determined at run-time). The created list is fixed-length if length is provided. The list has length 0 and is growable if length is omitted.

What is null safety in flutter?

Null safety means that a variable cannot have a null or void value. This feature improves user satisfaction by reducing errors and app crashes. Null safety ensures that all runtime null-dereference problems are shown at compile-time.

What is required keyword in dart?

The @required annotation marks named arguments that must be passed; if not, the analyzer reports a hint. With null safety, a named argument with a non-nullable type must either have a default or be marked with the new required keyword.


2 Answers

Short answer:

Instead of the pre-null-safety operations

var foo = List<int>();  // Now error var bar = List<int>(n); // Now error var baz = List<int>(0); // Now error 

use the following:

var foo = <int>[];           // Always the recommended way. var bar = List.filled(1, 0); // Not filled with `null`s. var baz = List<int>.empty(); 

Long answer:

The List constructor had two uses:

  • new List() to create an empty growable list, equivalent to [].
  • new List(n) to create a fixed-length list of length n filled with null values

With null safety, the second use was unsound most of the time, and there was no good way to fix it. It's possible to force a type argument to be non-nullable, but List<T>(4) only works when T is nullable. There is no way to enforce that.

So, the List(n) mode needed to go (replaced by List.filled(n, value) which forces you to provide a fill-value). That left List(), which doesn't really carry its own weight. You can just use [] instead (and you should!), so it was decided to remove the constructor entirely - all uses of it was either unsafe or useless. (Also, it was a weird constructor already, because if we wanted to properly make it null safe, it would have an optional parameter with a non-nullable type and no default value.)

By removing it completely, it makes it possible to, potentially, introduce a new List constructor in the future, perhaps as a shorter alias for List.filled. One can hope.

like image 196
lrn Avatar answered Sep 20 '22 13:09

lrn


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');   } 
like image 30
Lamech Desai Avatar answered Sep 18 '22 13:09

Lamech Desai