【问题标题】:flutter DropDownButtonFormField hint throws runtime errorflutter DropDownButtonFormField 提示抛出运行时错误
【发布时间】: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)(模拟器)

• 未发现任何问题!

【问题讨论】:

    标签: flutter dart


    【解决方案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),
      );
    }
    

    【讨论】:

    • 没有用。我还尝试提示另一个小部件,但仍然有相同的输出。
    猜你喜欢
    • 2021-12-22
    • 2021-09-15
    • 1970-01-01
    • 1970-01-01
    • 2021-07-11
    • 1970-01-01
    • 2017-06-06
    • 2022-08-23
    • 1970-01-01
    相关资源
    最近更新 更多