Repository navigation
Improve "DropdownButton's selectedItemBuilder" assertion exception #92773
Copy link
Copy link
Closed
Labels
a: error messageError messages from the Flutter frameworkError messages from the Flutter frameworkframeworkflutter/packages/flutter repository. See also f: labels.flutter/packages/flutter repository. See also f: labels.p: material_uimaterial_ui package in flutter/packagesmaterial_ui package in flutter/packagesteam-designOwned by Design Languages teamOwned by Design Languages team
Description
Run this:
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData.dark(),
home: Scaffold(
body: DropdownButtonHideUnderline(
child: DropdownButtonFormField<Color>(
selectedItemBuilder: (context) {
return [
DropdownMenuItem<Color>(
value: Colors.green,
child: RichText(
text: TextSpan(
children: [
WidgetSpan(
child: Padding(
padding: const EdgeInsets.only(
right: 10,
),
child: Icon(Icons.ac_unit),
),
baseline: TextBaseline.alphabetic,
alignment: PlaceholderAlignment.aboveBaseline,
),
const TextSpan(text: 'By '),
TextSpan(
text: 'test',
style: const TextStyle(
fontWeight: FontWeight.w700,
),
),
],
),
),
)
];
},
icon: SizedBox(),
value: Colors.accents[1],
decoration: const InputDecoration(
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Colors.transparent,
),
),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Colors.transparent,
),
),
labelStyle: TextStyle(
color: Colors.black,
fontWeight: FontWeight.w500,
fontSize: 14,
),
),
items: Colors.accents.map((value) {
return DropdownMenuItem<Color>(
value: value,
child: RichText(
text: TextSpan(
children: [
TextSpan(
text: value.toString(),
style: const TextStyle(
fontWeight: FontWeight.w700,
),
),
],
),
),
);
}).toList(),
onChanged: (value) {},
),
),
),
),
);
}Doctor:
[✓] Flutter (Channel stable, 2.5.3, on Microsoft Windows [Version 10.0.22000.282], locale en-US)
• Flutter version 2.5.3 at C:\flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision 18116933e7 (2 weeks ago), 2021-10-15 10:46:35 -0700
• Engine revision d3ea636dc5
• Dart version 2.14.4
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
• Android SDK at C:\Users\Agone\AppData\Local\Android\sdk
• Platform android-30, build-tools 30.0.3
• Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
• Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)
• All Android licenses accepted.
[✓] Visual Studio - develop for Windows (Visual Studio Community 2019 16.11.5)
• Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community
• Visual Studio Community 2019 version 16.11.31729.503
• Windows 10 SDK version 10.0.19041.0
[✓] Android Studio (version 2020.3)
• Android Studio at C:\Program Files\Android\Android Studio
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)
[✓] IntelliJ IDEA Ultimate Edition (version 2021.2)
• IntelliJ at C:\Program Files\JetBrains\IntelliJ IDEA 2021.2.2
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
[✓] VS Code (version 1.61.2)
• VS Code at C:\Users\Agone\AppData\Local\Programs\Microsoft VS Code
• Flutter extension version 3.27.0
[✓] VS Code (version 1.62.0-insider)
• VS Code at C:\Users\Agone\AppData\Local\Programs\Microsoft VS Code Insiders
• Flutter extension version 3.27.0
[✓] Connected device (1 available)
• Windows (desktop) • windows • windows-x64 • Microsoft Windows [Version 10.0.22000.282]
• No issues found!
Selected item disappears as a result.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
a: error messageError messages from the Flutter frameworkError messages from the Flutter frameworkframeworkflutter/packages/flutter repository. See also f: labels.flutter/packages/flutter repository. See also f: labels.p: material_uimaterial_ui package in flutter/packagesmaterial_ui package in flutter/packagesteam-designOwned by Design Languages teamOwned by Design Languages team
Activity
[-]using "DropdownButton's selectedItemBuilder" throws assertion exception[/-][+]using "DropdownButton's selectedItemBuilder" throws assertion exception (copy-paste reproducible code provided)[/+]or-adar commentedon Oct 31, 2021
Your problem is that the function assigned to "selectedItemBuilder" should return a list containing customized widgets corresponding to your items attribute under "DropdownButtonFormField",
meaning that they should both equal in size,
so when you pick a "DropdownMenuItem" the "DropdownButton" widget should display the corresponding widget residing at the same index as the selected item in items.
so the selectedItemBuilder property should look somewhat like this:
noga-dev commentedon Oct 31, 2021
Nice catch. This fixed the issue.
In retrospect the 'index' keyword should have tipped me off. Nonetheless, imo the error could be improved as I didn't care about the actual value selected or its siblings and only wanted to show a specific widget in the 'selectedItemBuilder.'
I'll leave this open for now to suggest a better way to modify the error message as I feel it's ambiguous enough to warrant a change in wording. Any team member is welcome to close this issue once my feedback has been acknowledged.
danagbemava-nc commentedon Nov 1, 2021
@agondev, can you share the assertion message that you got initially?
noga-dev commentedon Nov 1, 2021
Minimal reproduction error
@danagbemava-nc in this minimual reproduction there's an error that's missing when hovering over the dropdown.
Here it is with the onHover error (using Win11):
Non minimal reproduction error
danagbemava-nc commentedon Nov 2, 2021
Hi @agondev, how do you propose the error/assertion message be improved?
noga-dev commentedon Nov 2, 2021
[✔️]
"SelectedItemBuilder's index out of range (items.length != builder's children.length)"[✔️]
"Index out of range (are you sure items and selectedItemBuilder return the same list length"[❌]
Assertion should have been caught within the dropdown widget class.
danagbemava-nc commentedon Nov 2, 2021
Thanks for the suggestions
11 remaining items
flutter-triage-bot commentedon Mar 16, 2026
This issue is missing a priority label. Please set a priority label when adding the
triaged-designlabel.Framework: Improve DropdownButton selectedItemBuilder assertion (#183732
Framework: Improve DropdownButton selectedItemBuilder assertion (flut…
github-actions commentedon Apr 1, 2026
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of
flutter doctor -vand a minimal reproduction of the issue.Framework: Improve DropdownButton selectedItemBuilder assertion (flut…