Dart - Create and Initialize List in One Line Examples

If you want to find out how to create and initialize a List in Dart, read the examples in this tutorial.

Just like many programming languages, Dart supports List data type. The values of a List can be added dynamically, unless it's an unmodifiable List. Sometimes the values of a List can be determined at the time when it is created. In that case, it would be nicer if we can put initial values, especially in a one-line style. Dart provides several ways to do it. Below are the examples.

Define Initial Values

The most simple way to define a List in Dart is by using square brackets. It allows you to define the initial values between the brackets, with each value separated by comma.

  List<int> numbers = [];
  List<int> numbers = [1, 2, 3];

If you use any keyword like var, final, or const instead of specifying the variable type, you can define the List type by adding <Type> before the square brackets.

  var numbers = <int>[];
  var numbers = <int>[1, 2, 3];

Create From Existing List/Iterable

Dart also provides several ways to create a new List from an existing List or Iterable.

Using List.of

List.of can be used to create a List from an Iterable. It also has an optional growable named parameter which determines whether it's fixed-length or not.

  factory List.of(Iterable<E> elements, {bool growable = true})

Example:

  var originalList = <int>[1, 2, 3, 4];
  var newList = List.of(originalList);

Using List.unmodifiable

If you want to create a List that cannot be modified, you can use the unmodifiable factory method.

  factory List.unmodifiable(Iterable elements)

Example:

  var values = List.unmodifiable(['one', 'two', 'three']);

Using List.castFrom

List.castFrom can be used to create a new List variable from an existing List where the type of the new one is a supertype of the existing one. It works by adapting the source into a List<T> that refers to the same List, but with casted element type. That means adding or removing items will affect both source and new variables.

static List<T> castFrom<S, T>(List<S> source) => CastList<S, T>(source)

For example, we have Animal class and another class Cow that extends Animal.

  class Animal {
    String name;
  
    Animal({
      required this.name,
    });
  }
  
  class Cow extends Animal {
    double power;
  
    Cow({
      name,
      required this.power,
    }) : super(name: name);
  }

If we have a List whose element type is Cow, it's possible to create a new List variable whose element type is Animal. It works because a Cow instance is also an Animal instance.

  // Below works
  List<Cow> cows = [Cow(name: 'Peeko', power: 100), Cow(name: 'Bigfoot', power: 50)];
  List<Animal> animals = List.castFrom(cows);
  var animals = List.castFrom<Cow, Animal>(cows);

However, the other way may throw an error when an element is accessed. That's because an Animal element is not an instance of Cow.

  // Below doesn't work
  List<Animal> animals = [Animal(name: 'Peeko'), Animal(name: 'Bigfoot')];
  List<Cow> cows = List.castFrom(animals);
  var cow = cows[0]; // throw type 'Animal' is not a subtype of type 'Cow' in type cast

Using List.generate

You can also use List.generate which takes length as the first argument and a generator function as the second argument. The generator function which has a parameter index is responsible for generating the elements. An optional growable named parameter is also provided.

  factory List.generate(
      int length,
      E generator(int index),
      {bool growable = true}
  )

Example:

  var items = List.generate(5, (index) => 'item $index');

Using List.filled

Another way is by using List.filled. It's similar to generate, but for cases where all elements have the same initial value.

  factory List.filled(int length, E fill, {bool growable = false})

Example:

  var myList = List.filled(5, 1, growable: true); // List<int>
  List<double> myList = List.filled(5, 1, growable: true);

Summary

That's how to create a new List in Dart. You can either specify new values using square brackets or using any method provided by Dart to copy from an existing List or Iterable. Be careful if you use a mutable element type since modifying an element of the new List can affect the same element of the old List (and the other way round). You can also read our tutorial about how to copy a List in Dart which also explains about shallow copy and deep copy.