Dart
fixed-length list
programming
coding
data structures

What makes this a fixed-length list in Dart?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In Dart, a list is fixed-length when its size cannot be changed after creation. That does not mean the elements are immutable. It means operations that change the number of slots, such as add, remove, or insert, are not allowed. The key detail is usually whether the list was created with growable: false or by an API that returns a fixed-size list.

Fixed-Length Does Not Mean Read-Only

This is the first important distinction. A fixed-length list still lets you update existing positions:

dart
1void main() {
2  final values = List.filled(3, 0); // growable is false by default
3
4  values[0] = 10;
5  values[1] = 20;
6  values[2] = 30;
7
8  print(values); // [10, 20, 30]
9}

What you cannot do is change its length:

dart
1void main() {
2  final values = List.filled(3, 0);
3  values.add(40); // UnsupportedError
4}

So the fixed-length property is about structure, not about element mutability.

What Actually Makes It Fixed-Length

The most common reason is the constructor or factory you used. For example, List.filled creates a fixed-length list unless you explicitly pass growable: true.

dart
1void main() {
2  final fixed = List.filled(3, 0);
3  final growable = List.filled(3, 0, growable: true);
4
5  print(fixed.length);    // 3
6  print(growable.length); // 3
7}

At creation time both lists have length 3, but only the second one can later change size. That growable flag is the practical difference.

Other APIs also return fixed-length lists by design, especially when the result represents a known-size structure rather than a growable collection.

How to Tell in Practice

If you can assign to list[index] but add or remove throws UnsupportedError, you are dealing with a fixed-length list.

Example:

dart
1void main() {
2  final values = List<int>.filled(2, 0);
3
4  values[0] = 1;
5  print(values); // [1, 0]
6
7  try {
8    values.removeAt(0);
9  } catch (e) {
10    print(e.runtimeType); // UnsupportedError
11  }
12}

This behavior is intentional. Dart differentiates between changing element contents and changing collection size.

Fixed-Length Versus Unmodifiable

Another easy confusion is mixing up fixed-length lists with unmodifiable lists. They are not the same thing.

A fixed-length list:

  • allows replacing existing elements
  • does not allow changing length

An unmodifiable list:

  • does not allow changing elements
  • does not allow changing length

That means this is stricter than fixed-length:

dart
1void main() {
2  final values = List.unmodifiable([1, 2, 3]);
3  // values[0] = 10; // UnsupportedError
4}

If your code throws on element assignment itself, the list is likely unmodifiable, not merely fixed-length.

When Fixed-Length Lists Make Sense

Use fixed-length lists when the shape of the data is known in advance and should not change, such as:

  • RGB channels
  • game board rows with fixed width
  • small numeric vectors
  • lookup structures with stable size

They communicate intent. The code says, "this collection has exactly this many positions."

Common Pitfalls

  • Assuming fixed-length means the elements themselves cannot change.
  • Forgetting that List.filled is fixed-length unless growable: true is supplied.
  • Confusing fixed-length lists with unmodifiable lists.
  • Calling add, insert, or remove and being surprised by UnsupportedError.
  • Choosing a fixed-length list when the algorithm naturally needs to grow or shrink the collection.

Summary

  • A fixed-length list in Dart has a size that cannot change after creation.
  • You can usually still modify existing elements by index.
  • The most common cause is creating the list with growable: false, which List.filled uses by default.
  • Fixed-length is different from unmodifiable.
  • The deciding question is simple: can the number of elements change or not.

Course illustration
Course illustration

All Rights Reserved.