dart Collections Creating a new List

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

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.



Got any dart Question?