Skip to content
Justice Tech School

const and Unmodifiable Collections

const lists cannot change. List.unmodifiable wraps a copy.

Example
void main() {
  const a = [1, 2];
  var b = List.unmodifiable([3, 4]);
  try { b.add(5); } catch (e) { print("cannot add"); }
  print(a);
}

Exercise

Immutable.

 a = [1, 2];

Quiz Test

What happens if you add to a const list?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.