# Dart 3 in depth: Records

### Context

Yeah, Dart is amazing. What I like the most about this language is that it's so easy to use yet so powerful. There were many announcements at [Flutter Forward 2023](https://flutter.dev/events/flutter-forward#on-demand), among which was a new version of Dart: Dart 3. Since I started using Dart in 2019, the Dart team has kept adding a lot of exciting features continuously; the ones coming with Dart 3 are:

* Records
    
* Pattern matching
    
* New class modifiers
    
* FFI
    
* Support of WASM & Risk V
    
* etc...
    

> Browse [Dart 3 alpha announcement article](https://medium.com/dartlang/dart-3-alpha-f1458fb9d232) from the Dart team

The title has betrayed what will follow (I'm not going to let it ruin my transition anyway): this article will focus on the new Record feature in Dart 3.

### What is a Record?

You have probably already asked yourself how you could return 2 (or more) values from a function without creating a struct or a class. We have this typical case when working with geographic coordinates. Let's consider we want to return latitude, longitude and altitude from a function, we can find multiple ways to do this in Dart:

* **Using a** `Map`
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1677659352058/36b5fc59-3538-4dd8-8e5e-362ebae47191.png align="center")

* **Using a custom class**
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1677609518374/8f23d72a-b5d1-4390-a272-329f6244db00.png align="center")

* **Using a** `List`
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1677609563149/c32f64bc-4735-4d88-b734-b39c187e8aab.png align="center")

Let's go through the "issues" with these approaches:

* `Map` : They are error-prone since we can not guarantee all the desired values are returned.
    
* classes: It can become hectic to create a whole class for a such specific need. In some cases, you only want something simple
    
* `List` : As well as in the case of a `Map` , we cannot guarantee all the desired values are returned, and the access to values becomes sensitive because each time we will access a value, we will either check to List is large enough to possibly contain that value, or let the code live its life until an exception brings it to the hospital.
    

With Records, you'll be able to define multiple return types in a function at once. Let's take a look at the sample below :

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1677610323173/6573b6e7-0571-4360-b420-04e2c7a3b793.png align="center")

Our method returns 3 double values that can be accessed through their index. This is much better because it answers our need: return several values of a method simply and straightforwardly.

Granted, as written, this code is not pleasant to read. We don't know which value corresponds to what, and that's problematic.

We have two solutions to this problem: Destructuration and named values

#### Destructuration

Using destructuration, we can access values from our Record in a simpler way. It works just like destructuration in Javascript, Typescript, or Swift:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1677611350627/32e2c139-e8de-48f6-9377-432c02e4f8c4.png align="center")

#### Named values

Named values allow you to define a name for each value in your Record. Let's modify our previous sample to get demonstrate this:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1677610695366/244f902f-860a-4645-9062-59da5aadd12f.png align="center")

Much better, right?

We now have given a name to our parameters. It's easier to understand which value corresponds to what, and it's even easier to access the values.

You can have both named and positional parameters in the same Record:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1678117151483/5a864702-a2ef-4bcf-ad19-f0684cd8d582.png align="center")

Positional parameters can be accessed with the index (in the example above, `'Steve'` is at the index `$1` and `'France'` at the index `$2`

### Try this out

Dart 3 is still in alpha preview. You can give it a try in [Dartpad](https://dartpad.dev). It is disabled by default. Enable it by following the steps below:

* Open the dropdown on the bottom, near the "Send feedback" button
    
* ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1677608755483/d991f7cb-c1ce-4c91-bd34-e2a7398dc0f1.png align="center")
    
    Select "master channel"
    
* ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1677608832361/71f8a74e-f471-4a65-a860-a4b3316753c0.png align="center")
    

That's all. Easy, isn't it?

Thank you for reading.  

### Sources

* [https://github.com/dart-lang/language/blob/master/accepted/future-releases/records/records-feature-specification.md](https://github.com/dart-lang/language/blob/master/accepted/future-releases/records/records-feature-specification.md)
    
* [https://medium.com/dartlang/dart-3-alpha-f1458fb9d232](https://medium.com/dartlang/dart-3-alpha-f1458fb9d232)
