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

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(); (This kind of List Declaration Is already Deprecated in the Current version of Dart)

Short Answer 

Before Null-safety is Enabled in Dart we can use the below code to declare a List in Dart but this kind of Declaration in the Current Version of Dart thorough an Error,

Note that this Declaration Accepts Null while Null-safety is Enabled That is why they are Deprecated

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

Now Instead of the Above Declaration use the following to Declare a list in the Current Version of Dart

             var list =<int>[] //The Recommend ways of creating list in dart
             var list1  = List<int>.filled(1,0) //not filled with Null
             var List2= List<int>.empty();
             var list3= List<int>.filled(1,0,growable:false);
             var list4= List<int>.filled(0,0,growable:true);
             var list5= List<int>.generate(5,(i)=>i);


  We will see Each list Declaration in Detail,....

Long answer:

Now let us see why old form of list Declaration is Deprecated now a day??
The List constructor had two uses:

  1. new List() to create an empty growable list, equivalent to []. since it Accepts Null vlaue
  1. 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-null able, but List<T>(4) only works when T is Null able. (Because there is no initial value inside the list)  There is no way to enforce that. So, the List(n) form of list Declaration needed to go (replaced by    List.filled(n, value)   which forces you to provide a fill-value).
By removing it completely, it makes it possible to, potentially, introduce a new List constructor in the future.........

Introduction to List In Dart

Dart Lists

In Dart, list data type is used to represent a collection of objects. A List is an ordered group of objects. The List data type is actually similar to the concept of an array in other programming languages. An array is used to hold multiple values in single variable, similarly in Dart, an array is a List objects that hold multiple objects/values in single variable , so most people just call them lists. A list variable is defined by having values separated by commas and enclosed within square brackets ([]).

In Dart, lists can be classified as –

  • Fixed Length List
  • Growable List
A, Fixed Length list : In Dart, Fixed Length Lists are the lists which are defined with specific length. and cannot be changed over time

B  Growable List: A List object declared without size is termed as Growable List. The length of the growable list can changed in runtime.


➥➤ Syntax for Creating Fixed Length List

A,  If an initial size needs to be provided and there is a single reasonable initial value for the elements, then use List.filled:

var fixedList = new List<int>.filled(5, 0, growable: false);

B, If an initial size needs to be provided but each element needs to be computed, then use List.generate:

var fixedList = new List<int>.generate(5, (i)=>i);

➥➤ Syntax for Creating Growable List

var  growablelist= new <int>[]; //the recommended way of Creating grow able list.

var growableList = new List<int>.filled(00, growable: true); 

var growlist=List<int>.empty();

Now Let us Summerize what we have Descuss,

Summery

➤fixed-length-list Declaration in new version of dart

var list= <int>list.filled(5,0,growable: false); this is used only when the initial value is give or known

What if The initial value is not known and all the value of the list is obtained by some calculation we can use  list.generate()

var genaratedList= list.generate(5,(i)=>i); learn more about list.generate


➤Creating growable list in the Current Version of Dart

var growable=<int>[]; //which is called list listerals. 
it is the recomendedd way of creating growable List

we can also create a growable list like below

list growablelist= <int>list.filled(0,0,growable:true);

list newlist= List<int>.empty();

Thank you  for your Readding!!

No comments

Powered by Blogger.