Being able to work with Dart 3

Being able to work with Dart 3

If you are eager like me to meet with Dart 3 as soon as possible, you are in right place. I will try to speed up your process.

First of all, I want to declare that the solutions in this content are not special for Dart 3. You can apply these for other version usages.


Maybe you have been informed: the "stable" version of Dart 3 is planned around mid-2023, and the "alpha" version will be released about a month later (1). I guess it will be announced at the "Flutter Forward" event on January 25, 2023 (2).

As a Flutter/Dart developer I can't stand to experience new features. I know there are still 30 days to the alpha version announcement, but I predict that those new features won't be affected in this duration.


Most of us have already Flutter and Dart SDKs on our machines. If you want to try Dart3, no need to remove them. Getting experience with new SDK shouldn't affect ongoing projects which use current SDKs. So, the solution is to use multiple Dart SDKs on the same machine.

Note: for my scenario, my goal is to run the new "record" feature (3) on my VSCode without touching stable SDKs on my machine.


Locating Dart 3 SDK

Let's download the Dart3 SDK from Dart servers (4). (When I create this content, the recent version was 3.0.0-55.0.dev.)

After unzipping the SDK, it is time to move it to a safe folder. I think you have already this folder for your current Flutter/Dart SDKs.

Locating is completed, but we have a few steps to do.

Declaring SDK Paths to VSCode

Normally, if you are developing with Flutter, your VSCode is already auto-detecting the SDK locations.

Let's declare Dart 3 SDK. Open VSCode Settings (Code/Preferences/Settings) and search for "Dart SDK".

It is time to add an item for "Dart: Sdk Paths". For my case, the path is "/Users/leventkantaroglu/Development/dart3/dart-sdk".

With this usage, both SDKs can be used and are switchable easily. (You can still create and run projects on Dart SDK below 3.0)

Running First Project via Dart 3 SDK

You can run a new or existing project for this purpose, no matter what. And we have to switch to Dart 3 SDK as explained above. After that, you should update the environment value in pubspec.yaml and run dart pub get

environment:
  # sdk: '>=2.18.4 <3.0.0'
  sdk: 3.0.0-55.0.dev

If you press the "Run and Debug" button, your code will work. If you want to confirm that, you can run this code 👇😉

import 'dart:io' show Platform;

void main() {
  print(Platform.version);
}

//OUTPUT
//3.0.0-55.0.dev (dev) (Wed Dec 21 09:13:00 2022 -0800) on "macos_x64"
//Exited.

Running "record" Feature

Now we can run our codes with Dart 3 SDK. It is time to use the new "record" feature. Here is a little sample:

void main() {
  print("Size: ${getCurrentSize()}");
}

(int w, int h) getCurrentSize() {
    return (640, 480);
}

But Dart Analyzer is warning us about the "records" feature usage and we should enable this. There are ways to this problem (5). (I will continue to explain for VSCode flow.)

Create a launch.json file in the .vscode folder by VSCode or manually (if not exists) (6). And add/update toolArgs with --enable-experiment=records.

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "workshop_hello_dart3",
            "request": "launch",
            "type": "dart",
            "toolArgs": [
                "--enable-experiment=records",
            ],
        }
    ]
}

Here is the last step. Add/update this value to analysis_options.yaml file.

analyzer:
  enable-experiment:
    - records

Now we can press the "Run and Debug" button. And the result:

OUTPUT:
Size: (640, 480)
Exited.

You can switch back to the previous SDK or continue to enable other Dart features like this.

I hope Dart 3 satisfies you and your migrations can be made easily.

Thanks for reading :)
Levent Kantaroglu

Related Sources:
(1) https://medium.com/dartlang/the-road-to-dart-3-afdd580fbefa
(2) https://flutter.dev/events/flutter-forward
(3) https://github.com/dart-lang/language/blob/master/accepted/future-releases/0546-patterns/feature-specification.md
(4) https://dart.dev/get-dart/archive
(5) https://dart.dev/tools/experiment-flags
(6) https://dartcode.org/docs/launch-configuration/