Updating your pub package to Dart 2

Take inventory

Head over to pub and search for packages associated with your email. If you’ve been around the Dart ecosystem for a while (like me), you might have forgotten about ever writing some of these.

Decide which ones are worth updating

Some packages are timeless, and some not so much. For example, my simple_preprocessor package for adding C-style macros can safely die in Dart 1 land. It was useful at some point, but only to me, and not any more. Pub does a decent job of hiding these old packages from users, sorting them at the very bottom of everything. (In the future, we might add the possibility for package owners to flag their own packages with the [DEPRECATED] flag.)

These are all mine, and I’m happy for these to keep those scores.
You get to this visualization by clicking the last tab — the one that has the score in a circle.

Install Dart 2 (and 1)

If you’re using Flutter, you already have a preview release of Dart 2 (in $YOUR_FLUTTER_PATH/bin/cache/dart-sdk/bin/dart). But that's version 2.0.0-dev.58.0 so I recommend that you install the latest one via the standalone Dart SDK process. As of this writing, that will give you version 2.0.0-dev.69.1.

alias dart1=/path/to/dart-1.24.3-sdk/bin/dart
alias dartanalyzer1=/path/to/dart-1.24.3-sdk/bin/dartanalyzer
alias pub1=/path/to/dart-1.24.3-sdk/bin/pub
$ dart --version
Dart VM version: 2.0.0-dev.69.1 (Fri Jul 20 18:30:52 2018 +0200) on "macos_x64"
$ dart1 --version
Dart VM version: 1.24.3 (Wed Dec 13 23:26:59 2017) on "macos_x64"

Migrate the code

There’s a migration guide here: dartlang.org/dart-2. Go read it, it has really useful bits of information, like the existence of the dart2_fix tool, the dartfmt --fix flag, and many more.

  • Add .dart_tool/ into .gitignore
  • Bump the SDK version constraints in pubspec.yaml from something like sdk: ">=1.8.0 <2.0.0" to something like sdk: ">=2.0.0-dev <3.0.0" (or sdk: ">=1.24.3 <3.0.0" if you want to keep Dart 1 compatibility)
  • Bump all dependencies (and dev_dependencies) in pubspec.yaml to Dart-2-compatible versions (check their change log to find the version). Avoid pinning dependencies to the very latest version unless you absolutely need to. Having a broader range will give you package users more freedom to pick their own dependency versions.
  • Run pub upgrade (not just pub get). Even better, run _PUB_TEST_SDK_VERSION=2.0.0 pub upgrade (explained here).

Optional: add CI testing

If your package isn’t already continuously tested, now is a good chance to start. If you want to go with Travis, and assuming you have tests, then it’s pretty simple.

language: dart
dart:
- dev
script:
- dartanalyzer --fatal-warnings --fatal-lints .
- pub run test

Add a change log

While we’re at this, add a CHANGELOG.md file (assuming you don’t have it yet). Take this as a fresh start and just add the first record for this change, i.e. something like this:

## 1.2.3

- Upgrade to Dart 2

Bump your own version

If you only made some small internal changes, and no changes to your API, you can bump just the minor version. (At least that’s my reading of the semver spec.)

Upload to pub

Run pub publish.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Filip Hracek

Filip Hracek

Developer and manager working on Google’s Dart programming language and Flutter SDK; gamebooks enthusiast; https://filiph.net