Sub topic 10
Growable, Fixed-Length, Const, and Unmodifiable Collections
Assume you know Dart variables, generics such as List<int>, and null safety. In Dart, collections differ not only by their type, but also by whether you can change them. The main choices are growable, fixed-length, const, and unmodifiable collections.
Growable lists
A list literal creates a growable list by default. You can change both its elements and its length.
void main() {
final numbers = <int>[1, 2, 3];
numbers[0] = 10;
numbers.add(4);
numbers.removeAt(1);
print(numbers); // [10, 3, 4]
}The variable is declared with final, but the list is still mutable. final prevents assigning a different list to numbers; it does not freeze the list itself.
final numbers = <int>[1, 2];
numbers.add(3); // Allowed
// numbers = <int>[4, 5]; // Not allowedFixed-length lists
A fixed-length list lets you replace existing elements, but it does not let you add or remove elements.
void main() {
final values = List<int>.filled(3, 0);
values[1] = 7;
print(values); // [0, 7, 0]
try {
values.add(4);
} on UnsupportedError {
print('The list cannot change length');
}
}List.filled creates a fixed-length list unless you pass growable: true.
final fixed = List<int>.filled(3, 0);
final growable = List<int>.filled(3, 0, growable: true);Use a fixed-length list when the number of positions is part of the design, such as three color channels or twelve monthly values.
Const collections
A const collection is immutable. You cannot change its elements or its length. Dart can also share identical constant values, which makes constants useful for configuration and compile-time data.
void main() {
const names = <String>['Ada', 'Grace'];
print(names);
try {
names[0] = 'Katherine';
} on UnsupportedError {
print('A const list cannot be changed');
}
}The const applies to the list and its contents. A final list has a fixed reference but can still be changed.
| Declaration | Change elements? | Add or remove? |
|---|---|---|
| `final list = <int>[1, 2]` | Yes | Yes |
| `List<int>.filled(2, 0)` | Yes | No |
| `const <int>[1, 2]` | No | No |
Unmodifiable collections
List.unmodifiable creates an unmodifiable copy of another iterable. Attempts to change it throw UnsupportedError.
void main() {
final source = <String>['red', 'blue'];
final readOnly = List<String>.unmodifiable(source);
source.add('green');
print(source); // [red, blue, green]
print(readOnly); // [red, blue]
try {
readOnly.add('yellow');
} on UnsupportedError {
print('The read-only list cannot be changed');
}
}This is different from an unmodifiable view. A view prevents changes through that reference, but it can reflect changes made through the original list.
import 'dart:collection';
void main() {
final source = <int>[1, 2];
final view = UnmodifiableListView(source);
source.add(3);
print(view); // [1, 2, 3]
}Use List.unmodifiable when you need a snapshot. Use UnmodifiableListView when readers should see later changes but must not make changes themselves.
Choosing the behavior
When an API returns internal data, return an unmodifiable collection if callers must not alter that collection directly. When you need independent data, copy it with a growable literal or List.of.
final original = <int>[1, 2];
final copy = List<int>.of(original);
copy.add(3);
print(original); // [1, 2]
print(copy); // [1, 2, 3]Exercise
What is the difference between List.unmodifiable(source) and UnmodifiableListView(source)?
Show answer
List.unmodifiable stores an unmodifiable snapshot, while UnmodifiableListView reflects later changes to source but blocks changes through the view.