flutter DropDownButtonFormField 提示抛出运行时错误
Posted
技术标签:
【中文标题】flutter DropDownButtonFormField 提示抛出运行时错误【英文标题】:flutter DropDownButtonFormField hint throws runtime error 【发布时间】:2021-03-24 05:56:43 【问题描述】:我在使用 DropdownButtonFormField 时在运行时收到此错误:
════════ Exception caught by widgets library ═══════════════════════════════════
The following _TypeError was thrown building DropdownButton<int>(dirty, dependencies: [_InheritedTheme, Directionality, DropdownButtonHideUnderline, MediaQuery, _LocalizationsScope-[GlobalKey#c5725]], state: _DropdownButtonState<int>#eb5d0):
type 'DefaultTextStyle' is not a subtype of type 'Row' of 'value'
The relevant error-causing widget was
DropdownButtonFormField<int>
这是我的代码示例:
int _selectedSchoolId;
DropdownButtonFormField<int> _schlDropdown(List<School> _schools)
return DropdownButtonFormField(
value: _selectedSchoolId,
decoration: SMTextField.getDecoration(
context,
hintColor: Colors.white,
),
hint: Text(InAppMethods.getStr('select_school')),
selectedItemBuilder: (context) =>
_schools.map((e) => _school(e, true)).toList(),
items: _schools.map((e)
final String _name = Statics.isRTL() ? e.nameAr : e.nameEn;
final double _wdt = 30;
final bool _img = e?.image != null;
return DropdownMenuItem<int>(
child: Center(
child: Row(
children: [
if (_img)
Image.network(
e?.image,
width: _wdt,
height: _wdt,
),
SizedBox(
width: _img ? _wdt : _wdt * 2,
),
Text(
_name ?? '',
style: TextStyle(color: Colors.black),
),
],
),
),
value: e.id,
);
).toList(),
onChanged: (id) => setState(() => _selectedSchoolId = id),
);
这是我的颤振医生 -v 输出:
[✓] Flutter (Channel stable, 1.22.5, on Mac OS X 10.15.7 19H2 darwin-x64, locale en-TR) • Flutter 版本 1.22.5,位于 /Users/adnanfahed/flutter • 框架修订7891006299(3天前),2020-12-10 11:54:40 -0800 • 引擎修订版ae90085a84 • Dart 版本 2.10.4
[✓] android 工具链 - 为 Android 设备开发(Android SDK 版本 30.0.2) • Android SDK 位于 /Users/adnanfahed/Library/Android/sdk • 平台 android-30,构建工具 30.0.2 • Java 二进制文件位于:/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java • Java 版 OpenJDK 运行时环境(内部版本 1.8.0_242-release-1644-b3-6222593) • 接受所有 Android 许可证。
[✓] Xcode - 为 ios 和 macOS 开发 (Xcode 12.2) • Xcode 位于 /Applications/Xcode.app/Contents/Developer • Xcode 12.2,内部版本 12B45b • CocoaPods 版本 1.10.0
[✓] Android Studio(4.1 版) • Android Studio 位于 /Applications/Android Studio.app/Contents • 安装了 Flutter 插件 • Dart 插件版本 201.9245 • Java 版 OpenJDK 运行时环境(内部版本 1.8.0_242-release-1644-b3-6222593)
[✓] VS 代码(版本 1.52.0) • /Applications/Visual Studio Code.app/Contents 中的 VS 代码 • Flutter 扩展版本 3.17.0
[✓] 已连接设备(1 个可用) • sdk gphone x86 arm(移动) • emulator-5554 • android-x86 • Android 11 (API 30)(模拟器)
• 未发现任何问题!
【问题讨论】:
【参考方案1】:试试这个:
int _selectedSchoolId;
DropdownButtonFormField<int> _schlDropdown(List<School> _schools)
return DropdownButtonFormField(
value: _selectedSchoolId,
hint: Text(
InAppMethods.getStr('select_school',
style: TextStyle(color: Colors.white,)),
selectedItemBuilder: (context) =>
_schools.map((e) => _school(e, true)).toList(),
items: _schools.map((e)
final String _name = Statics.isRTL() ? e.nameAr : e.nameEn;
final double _wdt = 30;
final bool _img = e?.image != null;
return DropdownMenuItem<int>(
child: Center(
child: Row(
children: [
if (_img)
Image.network(
e?.image,
width: _wdt,
height: _wdt,
),
SizedBox(
width: _img ? _wdt : _wdt * 2,
),
Text(
_name ?? '',
style: TextStyle(color: Colors.black),
),
],
),
),
value: e.id,
);
).toList(),
onChanged: (id) => setState(() => _selectedSchoolId = id),
);
【讨论】:
没有用。我还尝试提示另一个小部件,但仍然有相同的输出。以上是关于flutter DropDownButtonFormField 提示抛出运行时错误的主要内容,如果未能解决你的问题,请参考以下文章
[Flutter] flutter项目一直卡在 Running Gradle task 'assembleDebug'...
flutter 日志输出,Flutter打印日志,flutter log,flutter 真机日志
Flutter开发 Flutter 包和插件 ( Flutter 包和插件简介 | 创建 Flutter 插件 | 创建 Dart 包 )
如何解决flutter gradle build error?C:\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 991