Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 601
Omar Fayad Avatar asked Mar 04 '21 14:03

Omar Fayad


People also ask

How do I enable null safety?

To enable null safety, you must go to your pubspec. yaml file and then navigates to the environment tag. You'll see that under the tag, there is an SDK tag version. Null Safety complies with the SDK version 2.12 and up.

How do you handle null safety in darts?

Use the null assertion operator ( ! ) to make Dart treat a nullable expression as non-nullable if you're certain it isn't null. is null, and it is safe to assign it to a non-nullable variable i.e.


2 Answers

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.

like image 129
Omar Fayad Avatar answered Oct 05 '22 23:10

Omar Fayad


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.

like image 22
fretman60 Avatar answered Oct 06 '22 00:10

fretman60