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
How to migrate your code depends on how old your code is and what platforms it runs on. For help with migrating web apps, see the web app migration guide. If you’re migrating a Flutter app, consult the breaking change notification. If you publish packages, then in addition to making platform-specific changes, follow 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
and the plugins for any IDEs you use.
- Flutter SDK upgrade instructions
- Dart SDK instructions (server-side 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
or use the problems view in Android Studio/IntelliJ or VS Code. - 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
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'
If you use features introduced after 2.0, be sure to specify the correct lower SDK constraint:
environment:
# Works in 2.1 but not 2.0.
sdk: '>=2.1.0 <3.0.0'
More resources
- DartPad
- Dart Language Specification
- 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