Python Records: Anonymous Composite Tuples in Dart 3
Introduced in Dart 3, **Records** (`(TypeA, {TypeB name})`) are anonymous, aggregate, immutable tuple values. Records allow functions to return multiple values type-safely without having to create throwaway DTO classes. Records support positional fields (`$1`, `$2`) and named fields (`rec.name`).
"A Record is like a partitioned bento box: you can hand someone a single box containing rice in compartment $1, salad in compartment $2, and salmon in the labeled "mainDish" compartment."
Deep Dive: How It Works
Anonymous & Strongly Typed: `(String, int)` is a concrete type with value equality out of the box.
Field Access: Positional fields are accessed via `$1`, `$2`, etc.; named fields are accessed by name (`record.fieldName`).
Value Equality: Two records with identical field values evaluate as equal (`==`) automatically.
Syntax Blueprint
// Return multiple values
(double lat, double lng) getCoordinates() {
return (37.7749, -122.4194);
}
// Usage with destructuring
final (lat, lng) = getCoordinates();Record return type and pattern destructuring.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Single-element positional record syntax `(42)` instead of `(42,)`.Why it happens: `(42)` is parsed as parenthesized integer expression 42.
How to fix: Include a trailing comma `(42,)` for single-element positional records.
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Return Min and Max as a Record
Define `(int min, int max) getBounds(List<int> list) => (list.reduce((a, b) => a < b ? a : b), list.reduce((a, b) => a > b ? a : b));`.
In `main()`, call `getBounds([10, 5, 20, 15])` and print `Min: ${b.$1}, Max: ${b.$2}`.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.