Flutter 2.x在GitHub Action中执行build_runner生成JSON序列化文件失败的问题排查及解决请求
build_runner Failure in GitHub Action for Flutter json_serializable Let's break down the critical issues from your build log and walk through how to fix them:
1. Incompatible json_annotation Version Constraint
Your log shows a severe error that's blocking the build:
[SEVERE] json_serializable:json_serializable on lib/material/button.dart: The version constraint "any" on json_annotation allows versions before 4.3.0 which is not allowed.
This happens because your current json_serializable version requires json_annotation at least v4.3.0, but your pubspec.yaml uses a loose any constraint that might pull in an older, incompatible version.
Fix:
- Open your
pubspec.yamlfile, locate thejson_annotationdependency, and update its version constraint to match whatjson_serializableexpects. For most recentjson_serializablev4.x releases, use:dependencies: json_annotation: '>=4.3.0 <5.0.0' - Make sure to run
flutter pub getbefore the build_runner command in your GitHub Action to fetch the correct dependency version.
2. Missing Part Directive for Generated File
Another critical issue that causes the final build failure is:
Warning: source_gen:combining_builder on lib/model/fav_music.dart: fav_music.g.dart must be included as a part directive in the input library with: part 'fav_music.g.dart';
The json_serializable generator requires every Dart file that uses @JsonSerializable() to explicitly declare the generated .g.dart file as a part.
Fix:
- Open
lib/model/fav_music.dart, and add this line after your imports and before your class definition:part 'fav_music.g.dart'; - Double-check that the file has the
@JsonSerializable()annotation on your class (otherwise the generator won't target it anyway).
3. Optional: Resolve Default Value Warnings
You'll also see warnings about conflicting default values in music_count.dart, like:
Warning: json_serializable:json_serializable on lib/repository/objects/music_count.dart: The constructor parameter for
artistCounthas a default value0, but theJsonKey.defaultValuevalue0will be used for missing ornullvalues in JSON decoding.
These warnings don't break the build, but they indicate redundant configuration. You can fix them by choosing one approach:
- Remove the constructor parameter's default value and keep only
JsonKey(defaultValue: 0)(this handles both missing fields andnullvalues in JSON) - Or remove the
JsonKey(defaultValue: 0)and keep the constructor's default value (this only applies when the field is missing from JSON, not when it's explicitlynull)
4. Update Your GitHub Action Workflow
Ensure your workflow steps prioritize dependency installation first to avoid version mismatches:
jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: subosito/flutter-action@v2 with: flutter-version: '2.x' # Match your project's Flutter version - name: Fetch dependencies run: flutter pub get - name: Generate JSON serialization files run: flutter pub run build_runner build --delete-conflicting-outputs
By addressing the first two issues (version constraint and missing part directive), your build_runner command should execute successfully in GitHub Actions. The warnings are optional but cleaning them up will make your codebase more consistent.
内容的提问来源于stack exchange,提问作者Dolphin

