2
votes

I am getting this error on runtime when using 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>

this is a sample of my code:

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),
  );
}

and this is my flutter doctor -v output:

[✓] Flutter (Channel stable, 1.22.5, on Mac OS X 10.15.7 19H2 darwin-x64, locale en-TR) • Flutter version 1.22.5 at /Users/adnanfahed/flutter • Framework revision 7891006299 (3 days ago), 2020-12-10 11:54:40 -0800 • Engine revision ae90085a84 • Dart version 2.10.4

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2) • Android SDK at /Users/adnanfahed/Library/Android/sdk • Platform android-30, build-tools 30.0.2 • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593) • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 12.2) • Xcode at /Applications/Xcode.app/Contents/Developer • Xcode 12.2, Build version 12B45b • CocoaPods version 1.10.0

[✓] Android Studio (version 4.1) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin installed • Dart plugin version 201.9245 • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)

[✓] VS Code (version 1.52.0) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.17.0

[✓] Connected device (1 available) • sdk gphone x86 arm (mobile) • emulator-5554 • android-x86 • Android 11 (API 30) (emulator)

• No issues found!

1

1 Answers

0
votes

Try this one:

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),
  );
}