dart Creating a new List


Lists can be created in multiple ways.

The recommended way is to use a List literal:

var vegetables = ['broccoli', 'cabbage'];

The List constructor can be used as well:

var fruits = new List();

If you prefer stronger typing, you can also supply a type parameter in one of the following ways:

var fruits = <String>['apples', 'oranges'];
var fruits = new List<String>();

For creating a small growable list, either empty or containing some known initial values, the literal form is preferred. There are specialized constructors for other kinds of lists:

var fixedLengthList1 = new List(8);
var fixedLengthList2 = new List.filled(8, "initial text");
var computedValues = new List.generate(8, (n) => "x" * n);
var fromIterable = new List<String>.from(computedValues.getRange(2, 5));

See also the Effective Dart style guide about collections.