Flutter Android应用编译失败:TextTheme的caption属性不存在
Flutter编译错误:TextTheme.caption不存在的解决方案
问题描述
时隔许久在Android设备上运行Flutter应用时触发编译错误,核心报错信息:
'TextTheme' 来自 'package:flutter/src/material/text_theme.dart'。
请尝试将名称更正为现有getter的名称,或者定义一个名为'caption'的getter或字段。
.caption!
^^^^^^^
Target kernel_snapshot failed: Exception
FAILURE: Build failed with an exception.
错误原因:任务 ':app:compileFlutterBuildDebug' 执行失败。进程 'command '/Users/iman/Documents/Dev/flutter/bin/flutter'' 以非零退出值1结束。
Flutter Doctor检测结果(Xcode相关问题不影响Android运行):
Doctor summary (to see all details, run flutter doctor -v): [✓] Flutter (Channel stable, 3.22.2, on macOS 13.3 22E252 darwin-arm64, locale en-US) [✓] Android toolchain - develop for Android devices (Android SDK version 35.0.0) [✗] Xcode - develop for iOS and macOS ✗ Xcode installation is incomplete; a full installation is necessary for iOS and macOS development. Download at: https://developer.apple.com/xcode/ Or install Xcode via the App Store. Once installed, run: sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer sudo xcodebuild -runFirstLaunch ✗ CocoaPods not installed. CocoaPods is used to retrieve the iOS and macOS platform side's plugin code that responds to your plugin usage on the Dart side. Without CocoaPods, plugins will not work on iOS or macOS. For more info, see https://flutter.dev/platform-plugins To install see https://guides.cocoapods.org/using/getting-started.html#installation for instructions. [✓] Chrome - develop for the web [✓] Android Studio (version 2024.1) [✓] VS Code (version 1.91.1) [✓] Connected device (3 available) [✓] Network resources
解决方案
你的Flutter版本是3.22.2(stable),该版本已完全移除TextTheme.caption属性,需替换为当前版本的对应API:
- 若原代码中
caption用于辅助说明文本(如注释、小字提示),替换为TextTheme.bodySmall - 若原代码中
caption用于小标签文本(如按钮旁的小型标签),替换为TextTheme.labelSmall
操作步骤
- 在项目代码中全局搜索
.caption!或TextTheme.caption - 将所有匹配项替换为对应的
.bodySmall!或.labelSmall! - 重新执行
flutter run启动应用
内容的提问来源于stack exchange,提问作者ErfanRahmati
相关产品推荐
相关产品推荐

