Flutter: List is deprecated?

FlutterDart

Flutter Problem Overview


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

Flutter Solutions


Solution 1 - Flutter

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']

Solution 2 - Flutter

List<MyClass> myList = <MyClass>[];

Solution 3 - Flutter

From:

_todoList = new List();

Change to:

_todoList = [];

Solution 4 - Flutter

old version

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

new version

List<Widget> widgetList = [];

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChrisView Question on Stackoverflow
Solution 1 - FlutterChrisView Answer on Stackoverflow
Solution 2 - FlutterSerdar PolatView Answer on Stackoverflow
Solution 3 - FlutterCybergigzView Answer on Stackoverflow
Solution 4 - FlutterTOPeView Answer on Stackoverflow