Flutter 颤振下拉菜单:初始化默认值异步
我们有一个Flatter项目正在运行,希望包含一个带有下拉菜单的有状态小部件。我们使用SQflite作为数据库 我们的数据库中有一个配置文件列表,通过下拉列表,我们希望选择一个配置文件。这个很好用 问题是: 我们使用SharedReferences保存在中选择的最后一个配置文件的ID。最初,我们希望使用最后选择的id(从SharedReferences加载)作为默认值的概要文件 我们的下拉菜单位于future builder中(属于类型为future的列表Flutter 颤振下拉菜单:初始化默认值异步,flutter,asynchronous,dart,stateful,Flutter,Asynchronous,Dart,Stateful,我们有一个Flatter项目正在运行,希望包含一个带有下拉菜单的有状态小部件。我们使用SQflite作为数据库 我们的数据库中有一个配置文件列表,通过下拉列表,我们希望选择一个配置文件。这个很好用 问题是: 我们使用SharedReferences保存在中选择的最后一个配置文件的ID。最初,我们希望使用最后选择的id(从SharedReferences加载)作为默认值的概要文件 我们的下拉菜单位于future builder中(属于类型为future的列表profilesFuture): 进一步
profilesFuture
):
进一步资料:
问题
怎么了?如何使用异步值正确初始化下拉菜单。问题肯定是由默认值引起的。如果不初始化value参数,它工作正常,但初始状态不正确
我们所要做的就是:
您应该检查项目中的值必须。值。“您需要签入的内容”下拉列表
List<DropdownMenuItem<String>> _bankAccountType = [
DropdownMenuItem(child: Text("--"), value: "--"),
DropdownMenuItem(child: Text("Checking"), value: "Checking"),
DropdownMenuItem(child: Text("Savings"), value: "Savings"),
];
DropdownButton<String>(
items: _bankAccountType,
iconSize: 0.0,
value: _bankAccountTypeValue,
onChanged: (value) {
setState(() {
_bankAccountTypeValue = value;
});
_checkBankAccountFormStatus();
}
),
List\u bankAccountType=[
DropdownMenuItem(子项:文本(“--”),值(“--”),
DropdownMenuItem(子项:文本(“检查”),值:“检查”),
DropdownMenuItem(子项:文本(“储蓄”),值:“储蓄”),
];
下拉按钮(
项目:\银行账户类型,
iconSize:0.0,
值:_bankAccountTypeValue,
一旦更改:(值){
设置状态(){
_bankAccountTypeValue=值;
});
_checkBankAccountFormStatus();
}
),
这将帮助您解决问题。这是解决我们问题的解决方案:
我们已经覆盖了数据模型配置文件中的默认==
操作
bool operator == (dynamic other) =>
other != null && other is Profile && this.id == other.id;
@override
int get hashCode => super.hashCode;
我们使用一个FutureBuilder
来处理上面提到的profilesFuture
(列表)。您的错误来自-很可能您的配置文件中没有覆盖==运算符和/或hashCode方法。我们应该覆盖它吗?因为什么?使用什么?因为assert-有一个检查:items.where((DropdownMenuItem item){return item.value==value;})。length==1
好的,我们已经检查过了。它解决了我们的问题。你想把这个作为一个答案,还是我应该通过引用你的评论来做呢?嘿,谢谢你的回答。但我要找的是你的水果类型[0]。我们必须首先将_currentFruchtype传递到菜单。。。但是在我们的网站上,它是异步的…明白了,让我来为你修正一下。pskink通过覆盖“==”-操作符和获取HashCode解决了我们的问题…嗯,这个HashCode
什么都不做-最好用id.HashCode
Done:)覆盖它也很好。非常感谢。
either zero or 2 or more dropdownmenuitems were detected with the same value. Assertion failed. There should be exactly one item with DropDownMenus value.
List<DropdownMenuItem<String>> _bankAccountType = [
DropdownMenuItem(child: Text("--"), value: "--"),
DropdownMenuItem(child: Text("Checking"), value: "Checking"),
DropdownMenuItem(child: Text("Savings"), value: "Savings"),
];
DropdownButton<String>(
items: _bankAccountType,
iconSize: 0.0,
value: _bankAccountTypeValue,
onChanged: (value) {
setState(() {
_bankAccountTypeValue = value;
});
_checkBankAccountFormStatus();
}
),
bool operator == (dynamic other) =>
other != null && other is Profile && this.id == other.id;
@override
int get hashCode => super.hashCode;