我有一个包含多个DropdownButtonFormField
的表单,除了一个表单外,其他所有表单都工作正常,这有点怪异,因为抛出的错误与一段时间后发现的错误无关,请进行故障排除。
错误为:(错误发生在选择项和setState上)
items == null || value == null || items.where((DropdownMenuItem item) => item.value == value).length == 1': is not true.
奇怪的是,它100%地确定了从列表中选择的值,尽可能不在列表中选择值!
此外,我已经使用了完全相同的DropdownButtonFormField
,但只使用了不同的数据!来自相同的API端点
因此,我开始看到这里的DropdownButtonFormField
与另一个DropdownButtonFormField
之间的区别!这就是我的数据长度!
有效的下拉菜单介于2到6个项目之间,而无效的下拉菜单超过50个项目!
所以我已经编辑了后端,并将数组减少到6个,然后DropdownButtonFormField
可以正常工作!但是,当然,这不是解决方案,我仍然需要下拉菜单中的所有+50个项目!!
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;
});
},
);
}
},
),
小部件的代码:
[✓] 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.
我扑扑的医生-v:
-
|_ /mysite/ app.js _ index
| |_ pag01, etc.
|
|_ /www/index.html
答案 0 :(得分:1)
dropdown.dart中的断言(正在触发错误消息)如下:
assert(items == null || items.isEmpty || value == null ||
items.where((DropdownMenuItem<T> item) {
return item.value == value;
}).length == 1,
'There should be exactly one item with [DropdownButton]\'s value: '
'$value. \n'
'Either zero or 2 or more [DropdownMenuItem]s were detected '
'with the same value',
),
如您所见,它告诉您列表中不存在所选项目(我认为只有在您编辑了字段中的值时才会发生),或者列表中存在重复的项目。我建议在代码之后立即在您的代码中添加一条打印语句
items:
snapshot.data.classes.map((collageYear) {
查看快照中返回的内容,然后将该输出与您认为的内容进行比较。
PS。我有一个包含250个项目的下拉列表(尽管来自常量值列表),可以正常工作,因此不是长度。它必须是数据问题。