【发布时间】:2020-03-01 01:38:39
【问题描述】:
我有一个包含多个 DropdownButtonFormField 的表单,除了一个之外,它们都工作正常,这有点奇怪,因为抛出一个与我在一段时间后发现的内容无关的错误,故障排除。
错误是:(错误发生在选择项和 setState 上)
items == null || value == null || items.where((DropdownMenuItem item) => item.value == value).length == 1': is not true.
奇怪的是,100% 确定选择的值来自列表,怎么可能选择不在列表中的值!
另外,我已经有完全相同的DropdownButtonFormField 工作正常,但只是使用不同的数据!来自同一个 API 端点
所以我开始看到这个DropdownButtonFormField 和另一个之间的区别!这是我的数据的长度!
工作的下拉列表介于 2 到 6 项之间,而无法正常工作并在选择时引发上述错误的下拉列表超过 50 项!
所以我编辑了我的后端并将数组减少到 6 个项目,DropdownButtonFormField 恢复正常工作!但当然,这不是一个解决方案,我仍然需要下拉列表中的所有 +50 个项目!
DropdownButtonFormField 小部件的代码:
FutureBuilder(
future: form,
builder: (BuildContext context,
AsyncSnapshot<Forms.Form> snapshot) {
if (!snapshot.hasData) {
return Padding(
padding: null,
child: Center(
child: CircularProgressIndicator(),
),
);
} else {
return DropdownButtonFormField(
isExpanded: true,
validator: (value) => value == null
? 'من فضلك اكمل الحقل المطلوب'
: null,
value: _selectedClass,
hint: Text('الفرقة'),
items:
snapshot.data.classes.map((collageYear) {
return DropdownMenuItem(
value: collageYear.value,
child: Text(collageYear.name),
);
}).toList(),
onChanged: (val) {
setState(() {
_selectedClass = val;
});
},
);
}
},
),
我的颤振医生-v:
[✓] Flutter (Channel stable, v1.12.13+hotfix.8, on Mac OS X 10.14.5 18F132, locale en-EG)
• Flutter version 1.12.13+hotfix.8 at /Users/esham/Development/flutter
• Framework revision 0b8abb4724 (3 weeks ago), 2020-02-11 11:44:36 -0800
• Engine revision e1e6ced81d
• Dart version 2.7.0
[!] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
• Android SDK at /Users/esham/Library/Android/sdk
• Android NDK location not configured (optional; useful for native profiling support)
• Platform android-29, build-tools 29.0.2
• ANDROID_HOME = /Users/esham/Library/Android/sdk
• ANDROID_SDK_ROOT = /Users/esham/Library/Android/sdk
• Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)
! Some Android licenses not accepted. To resolve this, run: flutter doctor --android-licenses
[✓] Xcode - develop for iOS and macOS (Xcode 11.3.1)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Xcode 11.3.1, Build version 11C504
• CocoaPods version 1.8.4
[✓] Android Studio (version 3.5)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin version 39.0.3
• Dart plugin version 191.8423
• Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)
[✓] VS Code (version 1.42.1)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.8.1
[✓] Connected device (1 available)
• Redmi Note 8 Pro • hev4t4bqo7hyvsnf • android-arm64 • Android 9 (API 28)
! Doctor found issues in 1 category.
【问题讨论】:
标签: flutter dart flutter-layout