You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter 2.x在GitHub Action中执行build_runner生成JSON序列化文件失败的问题排查及解决请求

Fixing 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.yaml file, locate the json_annotation dependency, and update its version constraint to match what json_serializable expects. For most recent json_serializable v4.x releases, use:
    dependencies:
      json_annotation: '>=4.3.0 <5.0.0'
    
  • Make sure to run flutter pub get before 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 artistCount has a default value 0, but the JsonKey.defaultValue value 0 will be used for missing or null values 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 and null values 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 explicitly null)

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 19:32:36