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 Sets


Share

Dart Sets

A set is a collection of items where every item is unique.

A set can never hold the same value twice.

You write a set with curly brackets {}, and separate the items with commas:

Example

void main() {
  var fruits = {'apple', 'banana', 'cherry'};
  print(fruits);
}
Try it Yourself »

Result:

{apple, banana, cherry}

Dart sees that all the items are strings, so the type of fruits is Set<String>.

You can also write the type yourself:

Example

void main() {
  Set<int> scores = {10, 20, 30};
  print(scores);
  print(scores.length);
}
Try it Yourself »

Result:

{10, 20, 30}
3

The length property tells you how many items the set has.


No Duplicates

If you put the same value in a set more than once, the set keeps only one copy:

Example

void main() {
  var colors = {'red', 'green', 'red', 'blue', 'green'};
  print(colors);
  print(colors.length);
}
Try it Yourself »

Result:

{red, green, blue}
3

This makes a set useful when you want to remove duplicates from a list.

The toSet() method turns a list into a set:

Example

void main() {
  var answers = ['yes', 'no', 'yes', 'yes', 'no'];
  var unique = answers.toSet();
  print(unique);
}
Try it Yourself »

Result:

{yes, no}


Empty Set or Empty Map?

Both sets and maps use curly brackets.

Because of this, an empty {} is not a set.

Dart reads an empty {} as an empty map:

Example

void main() {
  var things = {};
  print(things is Map);
  print(things is Set);
}
Try it Yourself »

Result:

true
false

To create an empty set, put the item type in angle brackets before the curly brackets, or write the type on the variable:

Example

void main() {
  var cars = <String>{};
  Set<String> bikes = {};
  print(cars is Set);
  print(bikes is Set);
}
Try it Yourself »

Result:

true
true

Add, Remove and Check Items

Use add() to add an item, and remove() to remove an item.

Use contains() to check if an item is in the set.

Example

void main() {
  var cars = {'Volvo', 'BMW'};
  cars.add('Ford');
  cars.remove('BMW');
  print(cars);
  print(cars.contains('Volvo'));
  print(cars.contains('BMW'));
}
Try it Yourself »

Result:

{Volvo, Ford}
true
false

The add() method returns true if the item was added.

It returns false if the item was already in the set:

Example

void main() {
  var cars = {'Volvo', 'BMW'};
  print(cars.add('Ford'));
  print(cars.add('Volvo'));
  print(cars);
}
Try it Yourself »

Result:

true
false
{Volvo, BMW, Ford}

Note: A set has no index, so you cannot write cars[0] like you do with a list.

Use a set when you only need to know if an item is there, and a list when the order and the position matter.


Union, Intersection and Difference

You can combine two sets in three ways:

  • union() returns all items from both sets
  • intersection() returns only the items that are in both sets
  • difference() returns the items in the first set that are not in the second set

Example

void main() {
  var tom = {'Math', 'English', 'Art'};
  var anna = {'Math', 'Music', 'Art'};

  print(tom.union(anna));
  print(tom.intersection(anna));
  print(tom.difference(anna));
}
Try it Yourself »

Result:

{Math, English, Art, Music}
{Math, Art}
{English}

These methods return a new set.

The original sets do not change.



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.