Dart 3 in depth: Records

Dart 3 in depth: Records

Discover the new "Record" type in Dart 3

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, 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 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

  • Using a custom class

  • Using a List

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 :

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:

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:

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:

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. It is disabled by default. Enable it by following the steps below:

  • Open the dropdown on the bottom, near the "Send feedback" button

  • Select "master channel"

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

Thank you for reading.

Sources

Did you find this article valuable?

Support Steve Nosse by becoming a sponsor. Any amount is appreciated!