未来<DropDownButton>项目为空/空/相同值 |扑动



在我的web应用程序的过程中,我希望用户允许进行更改并保存它们。在这个过程中,我使用SharedPreferences来存储更改。我有一个标题列表名为konzernDataTitle。一般来说,这个列表是用来显示我的标题的。

为了改变一些东西,我编辑了这个列表并"上传"。它到我的SharedPreferences。然而,一切工作正常,但我不能得到新的列表prefsKonzernTitle到我的下拉按钮。为此,我使用了FutureBuilder。错误很简单:

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
[...]
The following assertion was thrown building FutureBuilder<List<DropdownMenuItem<String>>>(dirty,
state: _FutureBuilderState<List<DropdownMenuItem<String>>>#dcca3):
Assertion failed:
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: MyTitle. nEither zero or 2 or more
[DropdownMenuItem]s were detected with the same value"
The relevant error-causing widget was:
FutureBuilder<List<DropdownMenuItem<String>>>

FutureBuilder功能:

Future<List<DropdownMenuItem<String>>> getDropDownItems() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
bool prefsTitleExist;
var newList = List<String>.empty();
if (prefs.containsKey("prefsKonzernTitle")) {
var getPrefList = prefs.getStringList("prefsKonzernTitle");
newList = getPrefList!;
prefsTitleExist = true;
} else {
prefsTitleExist = false;
}
final actualList = prefsTitleExist ? newList : konzernDataTitle;
return actualList.map((data) {
return DropdownMenuItem<String>(
value: data,
child: Text(
data,
),
);
}).toList();
}

FutureBuilder部件


FutureBuilder<List<DropdownMenuItem<String>>>(
future: getDropDownItems(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return const SizedBox();
}
return DropdownButton<String>(
value: dropdownValue,
items: snapshot.data,
onChanged: (String? newValue) {
setState(() {
dropdownValue = newValue!;
i = konzernDataTitle.indexOf(newValue);
titleController.text = konzernDataTitle[i];
linkController.text = konzernDataLink[i];
imageController.text = konzernDataImage[i];
colorController.text =
konzernDataColor[i].toString();
});
},
);
}),

我在列表中搜索了问题,但所有的列表都是他们必须的。所以也许你能帮我。谢谢你的帮助。所有问题将在几分钟内得到解答。

Tim^

应该只有一个[DropdownButton]的值:MyTitle。
0或2或更多[DropdownMenuItem]s被检测到相同的值

上述错误表示dropdownValue与下拉菜单项列表中可用的任何值或dropMenuItemList中存在的多个值不匹配。

对于第一种情况,在初始化时设置dropdownValue = null,对于第二种情况,检查菜单项是否有重复。

最新更新