iDecode
iDecode

Reputation: 28986

Why is Dart 2.12 a minor SDK upgrade when it breaks the previous code?

Currently, I'm have the following Dart SDK in my pubspec.yaml file.

environment:
  sdk: ">=2.6.0 <3.0.0"

But if I change it to the following and run flutter pub get

environment:
  sdk: ">=2.12.0 <3.0.0"

I start getting errors for nullability. As far as I know this is supposed to be a minor change according to language versioning but I broke my code. Should't the addition of Null safety in Dart SDK considered a major upgrade like 3.0.0 as it breaks the existing code.

Note: I didn't run any command to opt-in for null safety.

Upvotes: 1

Views: 400

Answers (2)

jamesdlin
jamesdlin

Reputation: 90135

Let me try wording Randal Schwartz's answer in a different way:

Using the Dart 2.12 SDK does not require using null safety. You can replace your existing Dart SDK installation with the Dart 2.12 SDK to use 2.12's tooling (e.g. to take advantage of better optimizations in the compiler, use new lints supported by the analyzer, new behavior in the formatter) and library implementations (i.e., to get bug fixes). If that's only thing that you do, your existing code should not be broken.

Although enabling null safety breaks old code, the null safety feature is opt-in. The mechanism to opt in to use null safety is to separately specify a minimum version of Dart 2.12 in your SDK requirements. When you specify:

environment:
  sdk: ">=2.6.0 <3.0.0"

that means that your code must remain compatible with Dart SDK version 2.6.0 (and therefore should not use any language features introduced after 2.6.0). Since the Dart 2.12 SDK can provide such backward compatibility, it did not technically require a major version bump.

Upvotes: 3

Randal Schwartz
Randal Schwartz

Reputation: 44186

Dart 2.12 didn't break the code. You did. If you change the minsdk to 2.12.0, it won't work until you fix everything, because it's now running (as you asked it to do) in NNBD mode. Dart 2.12 was happy to run it for you in non-null-safe mode, but you changed it. So, no, this is not a breaking change. Legacy code still runs fine with Dart running in legacy mode.

Upvotes: 0

Related Questions