Dart Records
last modified February 15, 2025
In this article, we show how to use records in Dart language. Records are an immutable, ordered, fixed-size collection of objects. They are useful for grouping multiple values into a single object without defining a custom class.
Records are similar to tuples in other programming languages. They allow you to return multiple values from a function or group related data together in a lightweight way.
Dart Records Simple Example
The following example demonstrates how to create and use a simple record in Dart.
void main() { // Creating a record final person = ('John', 30, true); // Accessing record elements print('Name: ${person.$1}'); print('Age: ${person.$2}'); print('Is Active: ${person.$3}'); }
In this program, we create a record containing a name, age, and active status. We then access and print each element of the record.
final person = ('John', 30, true);
We create a record with three elements: a string, an integer, and a boolean.
print('Name: ${person.$1}'); print('Age: ${person.$2}'); print('Is Active: ${person.$3}');
We access the elements of the record using the $1
, $2
, and $3
syntax.
$ dart main.dart Name: John Age: 30 Is Active: true
Dart Records with Named Fields
Records can also have named fields, which make the code more readable and self-explanatory.
void main() { // Creating a record with named fields final person = (name: 'Alice', age: 25, isActive: false); // Accessing record elements using named fields print('Name: ${person.name}'); print('Age: ${person.age}'); print('Is Active: ${person.isActive}'); }
In this program, we create a record with named fields and access the elements using the field names.
final person = (name: 'Alice', age: 25, isActive: false);
We create a record with named fields: name
, age
, and
isActive
.
print('Name: ${person.name}'); print('Age: ${person.age}'); print('Is Active: ${person.isActive}');
We access the elements of the record using the named fields.
$ dart main.dart Name: Alice Age: 25 Is Active: false
Dart Records in Functions
Records are particularly useful for returning multiple values from a function.
(String, int) getUserInfo() { return ('Bob', 40); } void main() { final userInfo = getUserInfo(); print('Name: ${userInfo.$1}'); print('Age: ${userInfo.$2}'); }
In this program, we define a function that returns a record containing a name and age. We then call the function and access the returned values.
(String, int) getUserInfo() { return ('Bob', 40); }
We define a function that returns a record with two elements: a string and an integer.
final userInfo = getUserInfo(); print('Name: ${userInfo.$1}'); print('Age: ${userInfo.$2}');
We call the function and access the returned record's elements.
$ dart main.dart Name: Bob Age: 40
Dart Records with Pattern Matching
Dart supports pattern matching, which allows you to destructure records into individual variables.
void main() { final person = ('Charlie', 35, true); // Destructuring the record final (name, age, isActive) = person; print('Name: $name'); print('Age: $age'); print('Is Active: $isActive'); }
In this program, we use pattern matching to destructure a record into individual variables.
final (name, age, isActive) = person;
We destructure the record into three variables: name
,
age
, and isActive
.
print('Name: $name'); print('Age: $age'); print('Is Active: $isActive');
We print the values of the destructured variables.
$ dart main.dart Name: Charlie Age: 35 Is Active: true
Source
Dart Records - Language Documentation
In this article, we have covered the basics of using records in Dart. Records are a powerful feature for grouping data in a lightweight and immutable way.
Author
List all Dart tutorials.