Flutter & .NET Blog

Expert insights on Flutter, Dart, and .NET development

Dart Immutable Collections

Dart Immutable Collections

Dart makes it pretty easy to create immutable classes, and several patterns encourage you to use them. However, we often don’t use immutable collections correctly. If a class has a collection property that is not immutable, the class is not actually immutable. It’s possible to add, remove or change one of the collection elements, which means we can mutate the overall class. For a class to be immutable, all members must be immutable, so collections must also be immutable. What is an Immutable Collection? An immutable collection is a collection that you can’t change. You cannot add, remove, modify the elements, or reorder the elements. The normal constructor of List<T> type does not qualify as immutable because it allows you to do all these things. For example, this class is not actually immutable because we can change elements in the strings...
Christian Findlay Christian Findlay Sep 29, 2022