How to create an empty list in Dart

I want to create an empty list for a Flutter project.

final prefs = await SharedPreferences.getInstance();
final myStringList = prefs.getStringList('my_string_list_key') ?? <empty list>;

I seem to remember there are multiple ways but one recommended way. How do I do it?


There are a few ways to create an empty list in Dart. If you want a growable list then use an empty list literal like this:

[]

Or this if you need to specify the type:

<String>[]

Or this if you want a non-growable (fixed-length) list:

List.empty()

Notes

  • In the past you could use List() but this is now deprecated. The reason is that List() did not initialize any members, and that isn't compatible with null safe code. Read Understanding null safety: No unnamed List constructor for more.
  • Effective Dart Usage Guide

Fixed Length List

 var fixedLengthList = List<int>.filled(5, 0);
 fixedLengthList[0] = 87;

Growable List

var growableList = [];
growableList.add(499);

For more refer Docs