Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

Dart Tutorial

Dart HOME Dart Intro Dart Get Started Dart Syntax Dart Output Dart Comments Dart Variables Dart final and const Dart Data Types Dart Numbers Dart Strings Dart String Interpolation Dart String Methods Dart Booleans Dart Type Conversion Dart Null Safety Dart late Dart Operators Dart Arithmetic Dart Assignment Dart Comparison Dart Logical Dart Type Test Dart Null Operators Dart Cascade Dart If...Else Dart Switch Dart While Loop Dart For Loop Dart Break/Continue

Dart Collections

Dart Lists Dart Access List Items Dart Change List Items Dart Loop Lists Dart List Methods Dart Sets Dart Maps Dart Access Map Items Dart Loop Maps Dart Collection if/for Dart Spread Operator

Dart Functions

Dart Functions Dart Parameters Dart Named Parameters Dart Return Values Dart Arrow Functions Dart Anonymous Functions Dart Closures Dart Scope Dart Recursion

Dart Classes

Dart OOP Dart Classes/Objects Dart Constructors Dart Named Constructors Dart Factory Constructors Dart Getters and Setters Dart Static Members Dart Inheritance Dart Abstract Classes Dart Interfaces Dart Mixins Dart Extension Methods Dart Enums Dart Generics

Dart 3 Features

Dart Records Dart Patterns Dart Switch Expressions Dart Sealed Classes Dart Class Modifiers

Dart Errors

Dart Exceptions Dart Custom Exceptions

Dart Async

Dart Async Intro Dart Futures Dart async/await Dart Streams Dart Isolates

Dart Libraries

Dart Imports Dart Packages Dart Math Dart Dates Dart JSON Dart RegExp Dart User Input Dart Files Dart Testing

Dart Examples

Dart Examples Dart Exercises


Dart Spread Operator


Share

Dart Spread Operator

The spread operator is three dots: ...

It takes all the items from one collection and puts them into a new list, set or map literal.

Example

void main() {
  var fruits = ['apple', 'banana'];
  var food = ['bread', ...fruits, 'cheese'];
  print(food);
}
Try it Yourself »

Result:

[bread, apple, banana, cheese]

Without the three dots, the whole list would be added as one item:

Example

void main() {
  var fruits = ['apple', 'banana'];
  var food = ['bread', fruits, 'cheese'];
  print(food);
  print(food.length);
}
Try it Yourself »

Result:

[bread, [apple, banana], cheese]
3

Join Lists

The spread operator is an easy way to join two or more lists into a new list:

Example

void main() {
  var cars = ['Volvo', 'BMW'];
  var moreCars = ['Ford', 'Mazda'];
  var allCars = [...cars, ...moreCars];
  print(allCars);
}
Try it Yourself »

Result:

[Volvo, BMW, Ford, Mazda]

The original lists do not change.

This also makes the spread operator a simple way to copy a list:

Example

void main() {
  var original = [1, 2, 3];
  var copy = [...original];
  copy.add(4);
  print(original);
  print(copy);
}
Try it Yourself »

Result:

[1, 2, 3]
[1, 2, 3, 4]


Sets and Maps

The spread operator works in set literals too.

The new set still has no duplicates:

Example

void main() {
  var tom = {'Math', 'Art'};
  var anna = {'Art', 'Music'};
  var subjects = {...tom, ...anna};
  print(subjects);
}
Try it Yourself »

Result:

{Math, Art, Music}

In a map literal, the spread operator adds all the key and value pairs.

If the same key appears twice, only the last value is kept:

Example

void main() {
  var defaults = {'theme': 'light', 'fontSize': 14};
  var userSettings = {'theme': 'dark'};
  var settings = {...defaults, ...userSettings};
  print(settings);
}
Try it Yourself »

Result:

{theme: dark, fontSize: 14}

The Null-Aware Spread ...?

You cannot spread a value that is null.

If the collection can be null, use the null-aware spread ...? instead.

It adds the items if the collection is not null, and adds nothing if it is null:

Example

void main() {
  List<String>? extras;
  var pizza = ['cheese', 'tomato', ...?extras];
  print(pizza);

  extras = ['ham', 'olives'];
  var bigPizza = ['cheese', 'tomato', ...?extras];
  print(bigPizza);
}
Try it Yourself »

Result:

[cheese, tomato]
[cheese, tomato, ham, olives]

If you use ... on a nullable list, the program does not run:

Example

void main() {
  List<String>? extras;
  var pizza = ['cheese', 'tomato', ...extras];
  print(pizza);
}
Try it Yourself »

Result:

main.dart:3:39: Error: An expression whose value can be 'null' must be null-checked before it can be dereferenced.
  var pizza = ['cheese', 'tomato', ...extras];
                                      ^

Spread with if and for

You can use the spread operator together with collection if, to add many items when a condition is true:

Example

void main() {
  var isAdmin = true;
  var adminPages = ['Users', 'Reports'];
  var menu = ['Home', 'Profile', if (isAdmin) ...adminPages];
  print(menu);
}
Try it Yourself »

Result:

[Home, Profile, Users, Reports]


Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.