Positional and Named Record Fields
Read record fields cleanly.
Positional and Named Record Fields is a free Dart Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Dart Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Two Ways to Hold Fields
A record can store positional fields, named fields, or both. The style you pick shapes how you read the values back out.
Positional Fields
List values plain, and they become positional fields. Order is what gives each one its meaning and place.
var pair = (3, 7);
print(pair);Reading Positional Fields
Access a positional field with a dollar sign and its index: $1 for the first, $2 for the second, and so on.
var pair = (3, 7);
print(pair.$1);Positions Start at One
Heads up: record positions begin at $1, not $0. There is no $0, so the first value is always $1.
Named Fields
Give a field a name with curly braces inside the parentheses. Names make a record self-explaining at a glance.
var p = (x: 3, y: 7);
print(p);Reading Named Fields
Access a named field by its name with a dot, just like a property. Clear, readable, and order does not matter here.
var p = (x: 3, y: 7);
print(p.y);Mixing Both Styles
You can combine them: positional first, then named. This blend gives you order where it helps and labels where they clarify.
var r = (200, status: 'OK');
print(r.status);Named Fields in the Type
The type annotation echoes the layout. Named fields appear inside braces with their names spelled out.
({int x, int y}) p = (x: 1, y: 2);
print(p.x);Names Boost Readability
When values could be confused, like two numbers, prefer named fields. lat: and lng: read far better than $1 and $2.
Field Names Are Part of the Type
Two records match only if their positional count and named field names match. Rename a field and you change the type.
Returning a Labeled Result
Functions love named fields for results. The caller reads .name instead of guessing what $1 meant.
({int w, int h}) size() => (w: 4, h: 3);
print(size().w);Quick Check
How do you read the first positional field of a record named pair?
Recap: Field Styles
Great progress! Use $1, $2 for positional fields and .name for named ones. Names win when clarity matters most. ✨
Frequently asked questions
Is the “Positional and Named Record Fields” lesson free?
Yes — the full text of “Positional and Named Record Fields” is free to read here on the web, and the Dart Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Dart Academy course, upgrade to CoddyKit PRO.
What will I learn in “Positional and Named Record Fields”?
Read record fields cleanly. You practise Dart Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Dart Academy?
No prior experience is required. Dart Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Positional and Named Record Fields” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Dart Academy lesson?
Yes. Every Dart Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Records: Lightweight Tuples
- Positional and Named Record Fields
- Destructuring With Patterns
- switch Patterns and Guards