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

Flutter+GitHub Actions+Fastlane构建报错:accentTextTheme未定义

Flutter CI/CD构建报错:ThemeData的accentTextTheme未定义

问题

我使用Flutter 3.16.7开发项目,正在搭建基于Fastlane和GitHub Actions的CI/CD流水线。本地Mac执行以下命令均能正常完成构建:

bundle exec fastlane deploy

或

flutter build app bundle --release

但GitHub Actions上构建时反复报错,提示ThemeData类的getter 'accentTextTheme'未定义。已尝试更换多个Flutter版本,累计执行近40次构建仍未解决。

报错信息

[15:00:45]: ▸ > Task :app:compileFlutterBuildDebug
[15:00:45]: ▸ ../../../.pub-cache/hosted/pub.dev/toggle_switch-0.1.9/lib/toggle_switch.dart:106:29: Error: The getter 'accentTextTheme' isn't defined for the class 'ThemeData'.
[15:00:45]: ▸ - 'ThemeData' is from 'package:flutter/src/material/theme_data.dart' ('/opt/hostedtoolcache/flutter/stable-3.16.7-x64/packages/flutter/lib/src/material/theme_data.dart').
[15:00:45]: ▸ Try correcting the name to the name of an existing getter, or defining a getter or field named 'accentTextTheme'.
[15:00:45]: ▸ ? Theme.of(context).accentTextTheme.bodyText1!.color
[15:00:45]: ▸ ^^^^^^^^^^^^^^^
[15:00:56]: ▸ Target kernel_snapshot failed: Exception
[15:00:56]: ▸ > Task :app:compileFlutterBuildDebug FAILED
[15:00:56]: ▸ FAILURE: Build failed with an exception.
[15:00:56]: ▸ * What went wrong:
[15:00:56]: ▸ Execution failed for task ':app:compileFlutterBuildDebug'.
[15:00:56]: ▸ > Process 'command '/opt/hostedtoolcache/flutter/stable-3.16.7-x64/bin/flutter'' finished with non-zero exit value 1
[15:00:56]: ▸ * Try:
[15:00:56]: ▸ > Run with --stacktrace option to get the stack trace.
[15:00:56]: ▸ > Run with --info or --debug option to get more log output.
[15:00:56]: ▸ > Run with --scan to get full insights.
[15:00:56]: ▸ > Get more help at https://help.gradle.org.

当前配置

FastFile

default_platform(:android)

platform :android do
  desc "Set Gradle Version and Build Number"
  lane :set_full_version do 
        version = flutter_version()

        android_set_version_name(version_name: version['version_name'])

        android_set_version_code(version_code: version['version_code'])
    end

  desc "Deploy a new version to the Google Play"
  lane :deploy do
    set_full_version

    gradle(task: "clean bundle")

    upload_to_play_store(
      aab: "../build/app/outputs/bundle/release/app-release.aab",
      track: "alpha"
    )
  end
end

GitHub Actions Workflow(省略无关步骤)

name: Deploy

on:
  push:
    branches:
      - dev

jobs:
  android:
    runs-on: ubuntu-latest
    env:
      BUNDLE_PATH: ../../../../.gem
      GRADLE_USER_HOME: ${{ github.workspace }}/.gradle
      FLUTTER_VERSION: '3.16.7'

    steps:
      ...
        
      - name: Setup Flutter
        uses: subosito/flutter-action@v2
        with:
          flutter-version: ${{ env.FLUTTER_VERSION }}

      - name: Get pub dependencies
        run: |
          flutter --version
          flutter doctor
          flutter clean
          flutter pub get  
     ...

      - name: Setup Fastlane
        uses: ruby/setup-ruby@v1
        with:
          ruby-version: 3.3
          rubygems: latest
          bundler-cache: false

      - name: Get bundler
        run: |
           gem install bundler -v 2.5.18

      - name: Build and deploy with Fastlane
        run: |
          cd android
          bundle install
          cd fastlane
          bundle exec fastlane deploy

解决方案

1. 升级老旧依赖(推荐)

报错根源是toggle_switch: 0.1.9版本过于老旧(发布于2019年),使用了Flutter 3.x已完全移除的accentTextTheme API。

  • 打开项目根目录的pubspec.yaml,将toggle_switch的版本更新为最新稳定版(如toggle_switch: ^2.1.0)。
  • 执行flutter pub upgrade toggle_switch更新依赖,提交pubspec.lock文件到仓库。

2. 清理GitHub Actions中的pub缓存

确保GitHub Actions拉取的是最新依赖,可在Get pub dependencies步骤中添加缓存清理命令:

flutter pub cache clean
flutter pub get

3. 临时修复(不推荐,仅用于无法升级依赖的场景)

通过patch工具修改老旧依赖的代码,替换废弃API:

  1. 在项目根目录创建patches/toggle_switch+0.1.9.patch文件,内容如下:
diff --git a/lib/toggle_switch.dart b/lib/toggle_switch.dart
index 9f2f3d7..a1b2c3d 100644
--- a/lib/toggle_switch.dart
+++ b/lib/toggle_switch.dart
@@ -103,7 +103,7 @@ class ToggleSwitch extends StatefulWidget {
               activeBgColors: activeBgColors,
               activeFgColor: activeFgColor ??
                   (isDarkMode
-                  ? Theme.of(context).accentTextTheme.bodyText1!.color
+                  ? Theme.of(context).textTheme.bodyText1!.color
                       : Theme.of(context).primaryColor),
               inactiveBgColor: inactiveBgColor ?? Colors.grey[200],
               inactiveFgColor: inactiveFgColor ?? Colors.grey[700],
  1. 添加patch依赖:flutter pub add patch
  2. 在GitHub Actions的flutter pub get步骤后执行:
flutter pub run patch apply

内容的提问来源于stack exchange,提问作者Moasfar Javed

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 22:42:08