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 Records


Share

Dart Records

A record groups several values together into one single value.

You create a record by writing the values inside parentheses (), separated by commas.

The values can have different types:

Example

void main() {
  var car = ('Volvo', 2020, true);
  print(car);
}
Try it Yourself »

Result:

(Volvo, 2020, true)

Each value in a record is called a field.

The record above has three fields: a string, an integer and a boolean.

Records are useful when you want to keep a few values together, but you do not want to write a whole class for them.

Note: Records were added in Dart 3.


Positional Fields

Fields without a name are called positional fields.

You get them by their position, with $1 for the first field, $2 for the second field, and so on:

Example

void main() {
  var car = ('Volvo', 2020, true);
  print(car.$1);
  print(car.$2);
  print(car.$3);
}
Try it Yourself »

Result:

Volvo
2020
true

Note: Positional fields start at $1, not at 0 like list indexes.



Named Fields

You can also give a field a name, with the syntax name: value.

These are called named fields, and you get them by their name:

Example

void main() {
  var person = (name: 'Liam', age: 30);
  print(person.name);
  print(person.age);
  print(person);
}
Try it Yourself »

Result:

Liam
30
(age: 30, name: Liam)

When you print a record, Dart shows the named fields in alphabetical order, so age comes before name.

A record can have both positional and named fields.

The positional fields are still counted with $1, $2, and the named fields are skipped in that count:

Example

void main() {
  var car = ('Volvo', 2020, electric: true);
  print(car.$1);
  print(car.$2);
  print(car.electric);
}
Try it Yourself »

Result:

Volvo
2020
true

Records cannot be changed after they are created.

The fields have getters, but no setters, so this program does not run:

Example

void main() {
  var person = (name: 'Liam', age: 30);
  person.age = 31;
}
Try it Yourself »

Result:

main.dart:3:10: Error: The setter 'age' isn't defined for the type '({int age, String name})'.
Try correcting the name to the name of an existing setter, or defining a setter or field named 'age'.
  person.age = 31;
         ^^^

If you need a different value, create a new record instead.


Record Types

Every record has a record type, which lists the types of its fields.

A record type looks like a record, but with types instead of values.

Named fields are written inside curly brackets {}:

Example

void main() {
  (String, int) fruit = ('Apple', 5);
  ({String name, int age}) person = (name: 'Emma', age: 25);
  (String, {bool electric}) car = ('Tesla', electric: true);

  print(fruit);
  print(person);
  print(car);
}
Try it Yourself »

Result:

(Apple, 5)
(age: 25, name: Emma)
(Tesla, electric: true)

Dart checks the types, just like for other variables.

A record with the wrong types or the wrong fields gives an error:

Example

void main() {
  (String, int) fruit = ('Apple', 'five');
  print(fruit);
}
Try it Yourself »

Result:

main.dart:2:25: Error: A value of type '(String, String)' can't be assigned to a variable of type '(String, int)'.
  (String, int) fruit = ('Apple', 'five');
                        ^

Return Several Values from a Function

A function can only return one value.

But that one value can be a record, so a function can return several values at the same time:

Example

(int, int) minMax(List<int> numbers) {
  var min = numbers.first;
  var max = numbers.first;
  for (var n in numbers) {
    if (n < min) min = n;
    if (n > max) max = n;
  }
  return (min, max);
}

void main() {
  var result = minMax([7, 2, 9, 4]);
  print('Lowest: ${result.$1}');
  print('Highest: ${result.$2}');
}
Try it Yourself »

Result:

Lowest: 2
Highest: 9

Named fields make the result easier to read:

Example

({String name, double average}) report(String name, List<int> scores) {
  var total = 0;
  for (var s in scores) {
    total += s;
  }
  return (name: name, average: total / scores.length);
}

void main() {
  var r = report('Anna', [80, 90, 70]);
  print('${r.name} has an average of ${r.average}');
}
Try it Yourself »

Result:

Anna has an average of 80.0

Tip: In the next chapter, you will see how to unpack the fields of a record into separate variables with patterns.


Record Equality

Two records are equal if they have the same shape and the same values.

The shape of a record is the number of positional fields and the names of the named fields.

The order of named fields does not matter:

Example

void main() {
  var a = ('Volvo', 2020);
  var b = ('Volvo', 2020);
  var c = ('Volvo', 2021);
  print(a == b);
  print(a == c);

  var p1 = (name: 'Liam', age: 30);
  var p2 = (age: 30, name: 'Liam');
  print(p1 == p2);
}
Try it Yourself »

Result:

true
false
true

This is different from classes, where two objects with the same values are not equal unless the class says so.



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.