Flutter升级后遇TextTheme及MediaQuery相关依赖错误求助
Flutter升级至最新版后API兼容错误解决指南
问题现象
升级Flutter到最新版本后出现以下编译错误:
TextTheme类未定义subtitle1/subtitle2getterScrollbar无isAlwaysShown参数MediaQuery.boldTextOverride成员未找到
具体错误日志:
/C:/Users/AppData/Local/Pub/Cache/hosted/pub.dev/dropdown_search-2.0.1/lib/dropdown_search.dart:491:46: Error: The getter 'subtitle2' isn't defined for the class 'TextTheme'. - 'TextTheme' is from 'package:flutter/src/material/text_theme.dart' ('/C:/src/flutter/packages/flutter/lib/src/material/text_theme.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'subtitle2'. style: Theme.of(context).textTheme.subtitle2, ^^^^^^^^^ /C:/Users/AppData/Local/Pub/Cache/hosted/pub.dev/dropdown_search-2.0.1/lib/dropdown_search.dart:515:46: Error: The getter 'subtitle1' isn't defined for the class 'TextTheme'. - 'TextTheme' is from 'package:flutter/src/material/text_theme.dart' ('/C:/src/flutter/packages/flutter/lib/src/material/text_theme.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'subtitle1'. style: Theme.of(context).textTheme.subtitle1); ^^^^^^^^^ /C:/Users/AppData/Local/Pub/Cache/hosted/pub.dev/dropdown_search-2.0.1/lib/src/popupMenu.dart:193:25: Error: The getter 'subtitle1' isn't defined for the class 'TextTheme'. - 'TextTheme' is from 'package:flutter/src/material/text_theme.dart' ('/C:/src/flutter/packages/flutter/lib/src/material/text_theme.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'subtitle1'. theme.textTheme.subtitle1; ^^^^^^^^^ /C:/Users/AppData/Local/Pub/Cache/hosted/pub.dev/dropdown_search-2.0.1/lib/src/selection_widget.dart:221:31: Error: No named parameter with the name 'isAlwaysShown'. isAlwaysShown: ^^^^^^^^^^^^^ /C:/src/flutter/packages/flutter/lib/src/material/scrollbar.dart:87:9: Context: Found this candidate, but the arguments don't match. const Scrollbar({ ^^^^^^^^^ /C:/Users/AppData/Local/Pub/Cache/hosted/pub.dev/dropdown_search-2.0.1/lib/src/selection_widget.dart:723:48: Error: The getter 'subtitle1' isn't defined for the class 'TextTheme'. - 'TextTheme' is from 'package:flutter/src/material/text_theme.dart' ('/C:/src/flutter/packages/flutter/lib/src/material/text_theme.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'subtitle1'. style: Theme.of(context).textTheme.subtitle1, ^^^^^^^^^ /C:/Users/AppData/Local/Pub/Cache/hosted/pub.dev/fl_chart-0.55.2/lib/src/utils/utils.dart:257:20: Error: Member not found: 'MediaQuery.boldTextOverride'. if (MediaQuery.boldTextOverride(context)) { ^^^^^^^^^^^^^^^^
根本原因
错误全部来自第三方依赖包版本过低,dropdown_search:2.0.1和fl_chart:0.55.2的内部代码使用了Flutter已废弃/移除的API,与最新版Flutter不兼容。你之前尝试的删除Android文件夹重建、更新Material Theme等操作仅针对项目自身代码,无法解决第三方包的内部问题。
解决方案
方案1:升级第三方依赖包(推荐)
- 打开项目根目录的
pubspec.yaml文件,找到对应依赖的版本声明。 - 将版本号更新为当前最新的稳定版:
dependencies: dropdown_search: ^5.0.6 fl_chart: ^0.65.0 - 执行以下命令更新依赖:
flutter pub upgrade - 若更新后仍有缓存问题,可清理缓存后重新获取:
flutter pub cache clean flutter pub get
方案2:临时兼容修复(不推荐长期使用)
如果因特殊原因无法升级包,可通过修改第三方包代码临时兼容:
- 将
TextTheme.subtitle1替换为TextTheme.bodyLarge,TextTheme.subtitle2替换为TextTheme.bodyMedium(Flutter 3.0+后文本样式API变更) - 将
Scrollbar(isAlwaysShown: true)替换为Scrollbar(thumbVisibility: true)(参数名变更) - 将
MediaQuery.boldTextOverride(context)替换为MediaQuery.of(context).boldText(静态方法移除)
内容的提问来源于stack exchange,提问作者sonalika
相关产品推荐
相关产品推荐

