Generator cannot target libraries that have not been migrated to null-safety

16,927

Solution 1

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.

Solution 2

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.

Solution 3

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

Solution 4

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

Share:
16,927
Omar Fayad
Author by

Omar Fayad

Mechanical and Software engineer.

Updated on June 06, 2022

Comments

  • Omar Fayad
    Omar Fayad about 2 years

    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.

  • Oliver Dixon
    Oliver Dixon over 3 years
    I'm stuck in werid dependancy hell. depends on both build_runner 1.12.2 and json_serializable 3.5.1, version solving failed.
  • zavora
    zavora about 3 years
    use build_runner: ^1.10.9
  • Abhitesh khatri
    Abhitesh khatri about 3 years
    use json_annotation: ^3.0.1, build_runner: ^1.0.0, json_serializable: ^3.3.0 it is working for me.