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
- Dart’s type system is now sound.
- Instance creation keywords are now generally optional,
as described in Using constructors:
-
new
is always optional. -
const
is optional inside of a constant context.
-
- Dart no longer has checked mode.
- Assert statements are still supported, but you enable them differently.
- The Dart language and core libraries have changed, partly as a result of the type system changes.
Tools
- Pub no longer supports transformers. Instead, use the new build system.
- Tools related to web development have changed.
- The new build system replaces
pub build
andpub serve
. - Dartium is no longer supported. Instead, use dartdevc and Chrome.
- The new build system replaces
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.
-
Get an up-to-date version of the Flutter or Dart SDK.
- Flutter SDK instructions
- Dart SDK instructions (VM or web)
-
Upgrade the packages your app depends on.
- Flutter:
flutter packages upgrade
- Server-side or web:
pub upgrade
- Flutter:
- Run the dart2_fix tool. It helps migrate some usages of deprecated Dart 1.x APIs to Dart 2.
-
Run the analyzer to find compile-time errors
and deprecation hints.
- Flutter:
flutter analyze
- Server-side or web:
dartanalyzer
- Flutter:
- Fix issues in your code and run the analyzer again, repeating until your code passes static analysis.
-
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.
- Fix issues until your code works.
-
Optional: Remove
new
and unnecessaryconst
.- You can remove these by hand or use a tool such as
dartfmt --fix
. - To find occurrences of
new
and unnecessaryconst
, add the rulesunnecessary_new
andunnecessary_const
to thelinter
section of your analysis options file.
- You can remove these by hand or use a tool such as
Each time the SDK has a significant release, repeat the process.
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:
# Works in Dart 1 (starting with 1.20.1), and works in Dart 2
sdk: '>=1.20.1 <3.0.0'
# Works in Dart 2 only, starting with Dart 2 dev build 61
sdk: '>=2.0.0-dev.61.0 <3.0.0'
More resources
- DartPad
- Dart 2 changes section of the Dart Language Specification page
- About Dart SDK release channels and version strings
- SDK constraints
- Updating your pub package to Dart 2, an article that includes tips for updating your code and using Travis to perform continuous integration (CI) testing