6 ways to copy a list or map in dart

Flutter tips Published on

Working with lists and maps in Dart is common. Sometimes you need to make a copy of one without changing the original. Here are several methods you can use.

Simple Ways to Copy

Using JSON encode/decode

This method creates a clone without reference by converting the list or map to a JSON string and then parsing it back.

import 'dart:convert';

List newList = json.decode(json.encode(oldList));
Map newMap = json.decode(json.encode(oldMap));

Using the Spread Operator (...)

Another way to clone without reference, often considered more readable and direct for simple cases.

List newList = [...oldList];
Map newMap = {...oldMap};
// For HashMap specifically
// HashMap newMap = HashMap.from({...oldMap});

Using the from Factory

This method creates a new list or map by copying values from the original. Note that for complex objects within the list/map, it might still copy references (shallow copy).

List newList = List.from(oldList);
Map newMap = Map.from(oldMap);

Using ..addAll

Similar to from, this method adds all elements from the original list to a new list. It's also a shallow copy.

List newList = []..addAll(oldList);
// For Map: Map newMap = {}..addAll(oldMap);

Using Packages for Specific Needs

Using Google built_collection package

This package is for creating immutable collections. It doesn't make an immediate deep copy but provides a copy-on-write wrapper and methods for rebuilding.

// Requires built_collection package
// var builtList = [1, 2, 3].build();
// builtList = builtList.rebuild((b) => b..addAll([7, 6, 5]));

Using package fast_immutable_collections

Another package for immutable collections. Provides immutable types like IList and IMap which are inherently "copied" when modified, as operations return new instances.

// Requires fast_immutable_collections package
// final IList<String> oldList; // example immutable list
// var newList = IList.orNull(oldList); // creates a new immutable list pointing to the same data initially

Choose the method that best fits your needs, considering if you need a deep clone, the performance implications, and if you are working with mutable or immutable collections.

Save 3 months of work

Create your app using our 6 years of making Flutter apps and more than 50+ apps

kickstarter for flutter apps

Frequently Asked Questions

What is the simplest way to copy a List or Map in Dart?

The spread operator (`[...]` for lists, `{...}` for maps) is often considered the most concise and readable way for simple copies.

Which methods create a deep copy (cloning without reference)?

The JSON encode/decode method creates a deep copy for simple data types. The spread operator also typically creates a deep copy for simple types. For complex nested objects, you might need manual deep copying or serialization methods.

Do List.from() and []..addAll() create deep copies?

No, `List.from()` and `..addAll()` perform shallow copies. They create a new list or map, but if the original contains complex objects, references to those objects are copied, not the objects themselves.

Why use packages like built_collection or fast_immutable_collections?

These packages are used when you need immutable collections. Operations on these collections return new instances rather than modifying the original, which can help prevent unexpected side effects in your code.

Read more
You may also be interested in
Flutter tips: how to create a responsive layout  blog card image
Flutter tips: how to create a responsive layout
Published on 2025-05-03
How to open the system app settings page  blog card image
How to open the system app settings page
Published on 2025-05-02
ApparenceKit is a flutter template generator tool by Apparence.io © 2025.
All rights reserved