What is a Record in Dart 3?
Records allow functions to bundle and return multiple typed values without having to create boilerplate class definitions.
Master modern Dart 3 language features: records, pattern matching, destructuring, and exhaustive sealed classes
Passing the Quiz & Exercises is mandatory to earn FX and proceed. If you fail, try again to gain the required FX, then the Proceed button will become available.
You can read this lesson publicly. Sign in to unlock exercises, quiz, and FX progress tracking.
Having trouble? AI is here to help
Ask context-aware questions about this lesson. Free plan: 5 questions/day. Upgrade to Premium for higher daily limits.
Use the AI button at the bottom of the screen
Dart 3 introduced the biggest evolution to the Dart language since Sound Null Safety. If you are building Flutter applications today, mastering Records, Pattern Matching, and Sealed Classes will make your code significantly more concise, expressive, and immune to entire categories of runtime bugs.
In this lesson, we will break down each feature with simple mental models and real-world Flutter examples.
Prior to Dart 3, if a function needed to return more than one piece of data (for example, coordinates x and y, or a user's name and age), you had two imperfect options:
Map<String, dynamic> — loose typing where typos cause runtime bugs.A Record is an anonymous, immutable, type-safe aggregate of values.
// Positional Record:
(double, double) getCoordinates() {
return (37.7749, -122.4194);
}
// Named Record:
({String name, int age}) getUserInfo() {
return (name: 'Sarah', age: 26);
}
Instead of accessing fields with index syntax, you can unpack them in a single readable line:
final (lat, lng) = getCoordinates();
print('Latitude: \$lat, Longitude: \$lng');
final (name: userName, age: userAge) = getUserInfo();
print('User: \$userName is \$userAge years old');
Records have built-in value equality! Two records with identical field values are automatically considered equal (== returns true) without writing custom hashcode or equals methods.
Dart 3 revolutionized switch from a verbose statement with manual break; lines into a high-powered expression that returns values directly.
// OLD WAY (Dart 2 statement):
String getStatusMessage(int statusCode) {
switch (statusCode) {
case 200:
return 'Success';
case 404:
return 'Not Found';
case 500:
return 'Server Error';
default:
return 'Unknown Status';
}
}
// MODERN DART 3 WAY (Switch Expression):
String getStatusMessage(int statusCode) => switch (statusCode) {
200 => 'Success',
404 => 'Not Found',
500 => 'Server Error',
_ => 'Unknown Status', // wildcard fallback
};
whenYou can attach conditions to patterns using when:
String formatAge(int age) => switch (age) {
< 0 => 'Invalid age',
>= 0 && < 13 => 'Child',
>= 13 && < 20 => 'Teenager',
_ when age >= 65 => 'Senior Citizen',
_ => 'Adult',
};
One of the most powerful patterns in modern Flutter state management is the Sealed Class.
When you mark a class with the sealed modifier:
When you use a switch expression over a sealed class hierarchy, Dart forces exhaustiveness. If you handle Loading and Success, but forget Error, the compiler will refuse to compile your code until you handle the missing state!
1// sealed_auth_state.dart2import 'package:flutter/material.dart';34// Sealed state hierarchy5sealed class AuthState {}6class AuthInitial extends AuthState {}7class AuthLoading extends AuthState {}8class AuthSuccess extends AuthState {9final String userName;10AuthSuccess(this.userName);11}12class AuthFailure extends AuthState {13final String message;14AuthFailure(this.message);15}1617// Clean, exhaustive Flutter UI builder:18Widget buildAuthScreen(AuthState state) => switch (state) {19AuthInitial() => const Center(child: Text('Please log in')),20AuthLoading() => const Center(child: CircularProgressIndicator()),21AuthSuccess(:final userName) => Center(child: Text('Welcome, \$userName!')),22AuthFailure(:final message) => Center(child: Text('Error: \$message', style: const TextStyle(color: Colors.red))),23// Notice: NO default (_) case! If a new state is added, the compiler alerts us immediately!24};
Avoid adding a default case (_ =>) when switching over a sealed class. Doing so defeats the compiler exhaustiveness check! When you add a new state subclass later, the compiler will not warn you if a wildcard catches it.
Master modern Dart 3 language features: records, pattern matching, destructuring, and exhaustive sealed classes. Dart is the language behind Flutter, and clear Dart code makes your Flutter UI easier to maintain.
Build confidence with basics first; speed comes later.
Prefer readable names, explicit intent, and async/await over deeply nested callbacks.
When Dart basics are weak, state bugs, null crashes, and async confusion increase quickly.
Records allow functions to bundle and return multiple typed values without having to create boilerplate class definitions.
Switch expressions evaluate to a value and return it directly. They use => syntax and do not require break statements.
Because all subclasses of a sealed class must reside in the same file, Dart guarantees compile-time exhaustiveness in switch expressions, preventing unhandled states.
Pattern destructuring unpacks the individual values of a record directly into individual named variables in a single expressive line.
Something seems incorrect, unclear, or missing? Let us know.
FlutterCraft editorial lessons are reviewed by experienced Flutter developers and updated to stay aligned with modern Flutter and Dart best practices.
Reference docs: Flutter Documentation, Dart Language Tour, and official package documentation where relevant.
Sign in to continue with exercises and quiz for this lesson.
Sign In to Continue