close
[Solved] Generator cannot target libraries that have not been migrated to null-safety

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

Hello Guys How are you all? Hope You all a re fine. When I try to upgrade my flutter app to flutter 2.0. I am Facing an Error. Flutter 2.0 upgrade, pub get failed with nonsense dependency behavior like Generator cannot target libraries that have not been migrated to null-safety. So today Here I come with all possible solutions for this error.

We are providing you all possible solutions to solve this error. let’s start this article without wasting your time.

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

When I am trying flutter pub run build_runner build –delete-conflicting-outputs this command I am faced below error in my terminal.

My flutter and dart version already updated, Here is my terminal log.

[SEVERE] json_serializable:json_serializable on lib/models/address/customer_address.dart

Generator cannot target libraries that have not been migrated to null-safety.
package:deals_and_delivery/models/address/customer_address.dart:9:7
  ╷
9 │ class CustomerAddress {
  │       ^^^^^^^^^^^^^^^
  ╵
[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

Here is my pubspec.yaml file

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

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

  1. How to solve Generator cannot target libraries that have not been migrated to null-safety ?

    After flutter upgraded Null Safety most of users having this issue with plugins. Actually 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.

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

    After flutter upgraded Null Safety most of users having this issue with plugins. Actually 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.

Solution 1

After flutter upgraded Null Safety most of users having this issue with plugins. Actually 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 Just You have to downgrade your package version

json_annotation: 3.1.1

and

json_serializable: 3.5.1

Solution 2

The problem is 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. So that you have to enable In your pubspec.yaml, if you enable null safety with the following:

environment:
   sdk: ">=2.12.0 <3.0.0"

Summery

So it’s all About All possible solutions. Hope this above all solution helped you a lot. Comment below Your thoughts and your queries. Comment Below on your suggestion.

Check Out Below Article

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *