Dart 2

Dart 2 has a few key differences from earlier versions of Dart. This page briefly covers those differences and gives general advice on migrating your code to Dart 2.

For information on why Dart 2 has changed, see the Dart 2 announcement.

Differences

The Dart language, libraries, build system, and web development tools have changed.

Language and libraries

Tools

Migrating your code

First, see the migration guide for your platform:

If you publish packages, then also see the package migration instructions below.

General process

Here’s an overview of the process of migrating to Dart 2, from either Dart 1.x or an earlier version of Dart 2.

  1. Get an up-to-date version of the Flutter or Dart SDK.
  2. Upgrade the packages your app depends on.
  3. Run the dart2_fix tool. It helps migrate some usages of deprecated Dart 1.x APIs to Dart 2.
  4. Run the analyzer to find compile-time errors and deprecation hints.
  5. Fix issues in your code and run the analyzer again, repeating until your code passes static analysis.
  6. Run tests to find runtime errors.
    • Run all automated tests for your software.
    • Do manual testing, and look for console errors. Consider adding automated tests to catch issues that you find.
  7. Fix issues until your code works.
  8. Optional: Remove new and unnecessary const.
    • You can remove these by hand or use a tool such as dartfmt --fix.
    • To find occurrences of new and unnecessary const, add the rules unnecessary_new and unnecessary_const to the linter section of your analysis options file.

Migrating packages

As a package owner, you need to do the following:

  • Follow the migration tips for the platforms that your package supports (see above).
  • Make sure your package passes Dart 2 analysis (see Run the analyzer above)
  • Make sure your package’s users know how to report issues.
  • Respond quickly to issue reports.
  • If code changes aren’t backward compatible, update the lower SDK constraint.

Changes and backward compatibility

If you have to change your package’s code, try to make it work in 1.x, as well as Dart 2. For example, you might be able to add type annotations or (if an API has been removed) to use an alternative 1.x API.

If a backward-compatible change isn’t possible, update the lower SDK constraint.

Test your changes to make sure that your package works as expected.

Upper constraints on the SDK version

Once your package passes Dart 2 analysis, update the upper constraint to declare that the package is compatible with Dart 2:

environment:
  # Works in Dart 2 only.
  sdk: '>=2.0.0 <3.0.0'

If you plan to maintain compatibility with older versions of Dart, adjust the lower SDK constraint accordingly:

environment:
  # Works in Dart 1 (starting with 1.20.1), and works in Dart 2.
  sdk: '>=1.20.1 <3.0.0'

More resources