64

After upgrading to the latest version of flutter, I get a deprecation warning for all my Lists.

List<MyClass> _files = List<MyClass>(); =>'List' is deprecated and shouldn't be used.

Unfortunately, it does not give a hint of what to replace it with. So what are we supposed to use instead now?

  • Dart SDK version: 2.12.0-141.0.dev
  • Flutter: Channel master, 1.25.0-9.0.pre.42
Hamed
  • 5,867
  • 4
  • 32
  • 56
Chris
  • 4,238
  • 4
  • 28
  • 49
  • 2
    see [list](https://master-api.flutter.dev/flutter/dart-core/List/List.html) – Anas Dec 13 '20 at 16:35
  • 3
    The lint means that the unnamed `List` *constructor* is deprecated, not the `List` type itself. – jamesdlin Dec 14 '20 at 01:07
  • This question here and its answers are much more clear than the other one there: https://stackoverflow.com/questions/63451506/the-default-list-constructor-isnt-available-when-null-safety-is-enabled-try – Alexandre Jean Sep 07 '21 at 05:41

4 Answers4

126

Ok, found it, it's just how to instantiate it:

List<MyClass> _files = [];

Edit: maybe the most common ones, a bit more detailed according to the docs:

Fixed-length list of size 0:

List<MyClass> _list = List<MyClass>.empty();

Growable list:

List<MyClass> _list = [];
//or
List<MyClass> _list = List<MyClass>.empty(growable: true);

Fixed length with predefined fill:

int length = 3;
String fill = "test";
List<String> _list =  List<String>.filled(length, fill, growable: true);
// => ["test", "test", "test"]

List with generate function:

int length = 3;
MyClass myFun(int idx) => MyClass(id: idx);
List<MyClass> _list = List.generate(length, myFun, growable: true); 
// => [Instance of 'MyClass', Instance of 'MyClass', Instance of 'MyClass']
jmizv
  • 1,172
  • 2
  • 11
  • 28
Chris
  • 4,238
  • 4
  • 28
  • 49
26
List<MyClass> myList = <MyClass>[];
Serdar Polat
  • 3,992
  • 2
  • 12
  • 12
9

From:

_todoList = new List();

Change to:

_todoList = [];
Cybergigz
  • 131
  • 1
  • 2
1

old version

 List<Widget> widgetList = new List<Widget>();

new version

List<Widget> widgetList = [];
TOPe
  • 113
  • 4