Description
Dart 3 & discontinuation of support for non-sound null safety
Background
In the Dart 2.12 release, Dart introduced sound null safety. When this was introduced we used an opt-in model: Only code that was opting into language version 2.12 or later was run with null safety. Dart developers could migrate their code in a stepwise fashion.
We are now planning a Dart 3 release -- tentatively slated for release by mid-2023 -- where we plan on only supporting Dart code that uses full sound null safety. This means that the null safety legacy mode will be discontinued, and that all code must run with full sound null safety.`
The reasons for making this change are:
-
Supporting both unsound and sound null safety adds overhead and complexity. Dart developers need to learn and understand both modes. Whenever reading a piece of Dart code, you need to check the language version to see if types are non-null by default (2.12 and later) or nullable by default (versions below 2.12)
-
Having to support both modes in our compilers and runtimes slows us down in terms of evolving the Dart SDK to support new features.
-
Our statistics on both packages on pub.dev and the null safety mode in which Flutter apps run, show that the ecosystem has largely already migrated to full sound null safety, and thus is ready 8000 to turn off unsound null safety/legacy mode.
Everything uses sound null safety in Dart 3
In Dart 3, only sound null safety is supported.
Concretely this implies that Dart 3 will not support:
-
Code using an SDK constraint where the min-constraint isn't at least 2.12 (e.g.
sdk: ">=2.7.0"
will be disallowed and won't resolve). -
Libraries using per-library language version markers selecting versions less than 2.12 (e.g.
// @dart=2.9
will be disallowed).
Backwards compatibility for null safe packages
Dart 3 sound null safety will be backwards compatible with with code already migrated to use null safety in language versions 2.12 and later.
This means that a Dart 3.0 SDK will allow SDK constraints where the lower-bound is at least 2.12
, even when the SDK upper-bound only allows versions less than 3 (<3.0
). For example, a package with the following constraint will be allowed to resolve with a Dart 3.x SDK:
environment:
sdk: '>=2.12.0 <3.0.0'
This allows developers to use Dart 3 sound null safety with packages that have been migrated to 2.12 null safety.
Note that Dart 3 may contain other breaking changes. For example some core library APIs are discontinued in Dart 3, which may mean that some packages may not be compatible with Dart 3 even if they have migrated to null safety with 2.12. We'll release a Dart 3 alpha release (see below) that can be used to test if code is affected by any of these breaking changes.
Roll out plan
We expect to roll out a Dart 3 alpha release several months prior to Dart 3 stable. This release will allow for testing if a package or app is compatible with Dart 3. The alpha release is not likely to contain other Dart 3 feature work unrelated to sound null safety, but will contain all planned breaking changes.
Later we also plan on releasing a Dart 3 beta release that is more feature complete.
Finally, we'll release the Dart 3.0 stable release, tentatively by mid-2023.
Work item tracking
Dart 2.19 stable, and associated Flutter stable
- When
dart
orflutter
commands run or build, without sound null safety, emit an Info message:Info: not using sound null safety, which is the only supported Dart 3 mode. See http://dart.dev/null-safety for details.
Dart 3 alpha
-
Update Dart version number to
3.0.x
: https://dart-review.googlesource.com/c/sdk/+/271922 -
When the
dart
andflutter
commands run or build, without sound null safety (i.e. any language version below 2.12), emit an error:Error: not using sound null safety, which is the only supported Dart 3 mode. See http://dart.dev/null-safety for details.
-
Update
pub
solver to allow for resolving packages with SDK constraints
likesdk: '>=2.12.0 <3.0.0'
to match SDK 3.0.0, to enable Dart 2.12 and
later backwards compatibility:
pub client: update solver to support language version 2.12 in Dart 3 pub#3554 -
Complete VM updates: Flip the Dart VM to run in sound null safety mode by default and deprecate legacy null safety mode #50093
Dart 3 beta
-
Clean up the older null safety CLI messages: Remove "Building with sound null safety" output during
run
andbuild
flutter/flutter#115803 -
Remove support for legacy
--no-sound-null-safety
flag:
[VM] Hide unsound null safety flag if build requires it #50348 -
Support Dart 3 packages on pub.dev:
"Dart 3 ready" badge on pub.dev pub-dev#6023,
Analyze packages on pub.dev with Dart 3 alpha release pana#1123,
Dart 3 compatible search filter on pub.dev pub-dev#6022 -
Remove
null safety
badge from generated API docs:
Discontinue null safety badge dartdoc#3294