@@ -176,12 +176,12 @@ of their fields.
176
176
177
177
Records allow functions to return multiple values bundled together.
178
178
To retrieve record values from a return,
179
- destructure the values into local variables using [ pattern matching] [ pattern ] .
179
+ [ destructure] [ ] the values into local variables using [ pattern matching] [ pattern ] .
180
180
181
181
<? code-excerpt "language/test/records_test.dart (record-multiple-returns)"?>
182
182
``` dart
183
183
// Returns multiple values in a record:
184
- (String, int) userInfo(Map<String, dynamic> json) {
184
+ (String name , int age ) userInfo(Map<String, dynamic> json) {
185
185
return (json['name'] as String, json['age'] as int);
186
186
}
187
187
@@ -191,7 +191,7 @@ final json = <String, dynamic>{
191
191
'color': 'blue',
192
192
};
193
193
194
- // Destructures using a record pattern:
194
+ // Destructures using a record pattern with positional fields :
195
195
var (name, age) = userInfo(json);
196
196
197
197
/* Equivalent to:
@@ -201,6 +201,18 @@ var (name, age) = userInfo(json);
201
201
*/
202
202
```
203
203
204
+ You can also destructure a record using its [ named fields] ( #record-fields ) ,
205
+ using the colon ` : ` syntax, which you can read more about on the
206
+ [ Pattern types] [ ] page:
207
+
208
+ <? code-excerpt "language/test/records_test.dart (record-name-destructure)"?>
209
+ ``` dart
210
+ ({String name, int age}) userInfo(Map<String, dynamic> json)
211
+ // ···
212
+ // Destructures using a record pattern with named fields:
213
+ final (:name, :age) = userInfo(json);
214
+ ```
215
+
204
216
You can return multiple values from a function without records,
205
217
but other methods come with downsides.
206
218
For example, creating a class is much more verbose, and using other collection
@@ -217,3 +229,5 @@ parallelization of futures of different types, which you can read about in the
217
229
[ pattern ] : /language/patterns#destructuring-multiple-returns
218
230
[ `dart:async` documentation ] : /libraries/dart-async#handling-errors-for-multiple-futures
219
231
[ parameters and arguments ] : /language/functions#parameters
232
+ [ destructure ] : /language/patterns#destructuring
233
+ [ Pattern types ] : /language/pattern-types#record
0 commit comments