Lists
Creating, modifying, and transforming ordered collections with Dart's List type.
អាន 2 នាទី
A List is Dart's ordered collection — the equivalent of an array in most other languages, except it grows and shrinks dynamically by default.
Creating a list
List<String> fruits = ['apple', 'banana', 'cherry'];
var numbers = <int>[1, 2, 3]; // explicit type argument, inferred element type either way
print(fruits[0]); // apple
print(fruits.length); // 3The <String> in List<String> is a generic type argument — it tells the compiler every element must be a String, so fruits.add(42) fails to compile rather than silently corrupting your data. You'll see this same <Type> syntax on every collection type in Dart.
Adding, removing, and checking contents
final tasks = <String>[];
tasks.add('Write lesson');
tasks.add('Review code');
tasks.addAll(['Test app', 'Deploy']);
print(tasks); // [Write lesson, Review code, Test app, Deploy]
tasks.remove('Review code');
tasks.removeAt(0);
print(tasks.contains('Deploy')); // true
print(tasks.isEmpty); // falseGrowable vs. fixed-length lists
By default, list literals like ['a', 'b'] are growable. You can also create a fixed-length list, which rejects .add() and .remove() outright:
final fixed = List<int>.filled(3, 0); // [0, 0, 0], length locked at 3
fixed[1] = 5; // fine — updating an existing index is allowed
// fixed.add(4); // throws at runtime: Unsupported operationReach for a fixed-length list when the size is genuinely known ahead of time and you want the extra safety of catching an accidental .add() — otherwise, the plain growable list is what you want almost always.
Transforming lists without loops
Dart's collection methods let you describe what transformation you want instead of writing the loop that performs it:
final numbers = [1, 2, 3, 4, 5, 6];
final doubled = numbers.map((n) => n * 2).toList();
final evens = numbers.where((n) => n.isEven).toList();
final sum = numbers.fold<int>(0, (total, n) => total + n);
print(doubled); // [2, 4, 6, 8, 10, 12]
print(evens); // [2, 4, 6]
print(sum); // 21.map() transforms each element, .where() filters, and .fold() reduces the whole list to a single value by carrying an accumulator through each element. All three return a lazy iterable rather than a list directly, which is why .map() and .where() above end with .toList() — that forces the transformation to actually run and collects the results into a concrete List.
Collection-if and collection-for
Dart lets you embed conditionals and loops directly inside a list literal:
bool includeExtras = true;
final items = [
'base item',
if (includeExtras) 'bonus item',
for (var i = 1; i <= 3; i++) 'generated $i',
];
print(items); // [base item, bonus item, generated 1, generated 2, generated 3]This comes up constantly in Flutter widget trees, where you often need to conditionally include a child without breaking out of the list literal to do it — but it's just as useful for building any list whose contents depend on some runtime condition.