Generator cannot target libraries that have not been migrated to null-safety
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.
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.
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.1json_serializable: ^4.1.2build_runner: ^2.0.3
After that, run:
flutter pub getflutter pub run build_runner build --delete-conflicting-outputs