43

This problem occurs when trying the following command:

flutter pub run build_runner build --delete-conflicting-outputs

The error message:

[SEVERE] json_serializable:json_serializable on lib/models/shipping_address/shipping_address.dart:

Generator cannot target libraries that have not been migrated to null-safety.
package:deals_and_delivery/models/shipping_address/shipping_address.dart:6:7
  ╷
6 │ class ShippingAddress {
  │       ^^^^^^^^^^^^^^^
  ╵
[INFO] Running build completed, took 3.6s

[INFO] Caching finalized dependency graph...
[INFO] Caching finalized dependency graph completed, took 49ms

[SEVERE] Failed after 3.7s
pub finished with exit code 1

pubspec.yaml:

dependencies:
  json_annotation: ^4.0.0
  flutter:
    sdk: flutter
...
dev_dependencies:
  build_runner: ^1.11.5
  json_serializable: ^4.0.2
  flutter_test:
    sdk: flutter

These are the current flutter and dart versions:

[√] Flutter (Channel stable, 2.0.0, on Microsoft Windows [Version
> 10.0.19042.844], locale en-US)
>     • Flutter version 2.0.0 at C:\flutter
>     • Framework revision 60bd88df91 (22 hours ago), 2021-03-03 09:13:17 -0800
>     • Engine revision 40441def69
>     • Dart version 2.12.0

I am stuck at this point, how to solve this problem.

Omar Fayad
  • 1,733
  • 3
  • 11
  • 27

6 Answers6

62

I found out that json_serializable >=4.0.0 depends on json_annotation >=4.0.0 <4.1.0 and the json_annotation: ^4.0.0 includes Null Safety but json_serializable: ^4.0.2 does not, so the error is occurring.

So I downgraded both packages:

json_annotation: 3.1.1

and

json_serializable: 3.5.1

And they work again properly.

Omar Fayad
  • 1,733
  • 3
  • 11
  • 27
30

I think this all depends on if you are intending on upgrading your overall project to enable null safety or not. If you want to use the latest json_serializable packages (that have enabled null safety), you will need up specify it as such in your environment.

In your pubspec.yaml, if you enable null safety with the following:

environment:
   sdk: ">=2.12.0 <3.0.0"

... then the latest json_serializable packages should work without issue.

Reference: Behind the scenes: SDK constraints

Chances are, your "sdk" environment is something less than 2.12.0 if they are giving you that error.

However, if you are not interested in updating for null safety, then you will likely need to keep your associated json_serializable packages downgraded as you mentioned.

fretman60
  • 351
  • 2
  • 2
9

In your pubspec.yaml file, the lower SDK version should be >=2.12.

environment:
  sdk: '>=2.12.0 <3.0.0'

Use the following versions (all are null-safe)

json_annotation: ^4.0.1
json_serializable: ^4.1.2
build_runner: ^2.0.3

After that, run:

flutter pub get
flutter pub run build_runner build --delete-conflicting-outputs
iDecode
  • 22,623
  • 19
  • 99
  • 186
6

Try to set:

sdk: '>=2.12.0 <3.0.0'

it works for me when I use json_serializable: ^4.0.1 and json_anotation: ^4.0.1

Grigory Zhadko
  • 1,484
  • 1
  • 19
  • 33
0

I got the same problem and solve it with below steps:

  1. Set sdk: '>=2.12.0 <3.0.0'

  2. Change version of json_serializable and json_annotation to ^4.0.0 (not 4.1.0)

Dharman
  • 30,962
  • 25
  • 85
  • 135
Jenny Tran
  • 553
  • 4
  • 10
0

Upgrade your pubspec file,

  sdk: '>=2.12.0 <3.0.0'

  json_annotation: ^4.0.1
  json_serializable: ^4.1.0
  build_runner: ^1.12.2

After upgrade the packages to run this command:

flutter pub run build_runner build --delete-conflicting-outputs
Rudresh Narwal
  • 2,740
  • 3
  • 11
  • 21