- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在Flutter中创建一个下拉按钮。我从数据库中获取一个列表,然后将该列表传递给我的dropdownButton
一切正常数据显示为预期的,但是当我从中选择一个元素时出现此错误:
There should be exactly one item with [DropdownButton]'s value: Instance of 'Tag'.
Either zero or 2 or more [DropdownMenuItem]s were detected with the same value
'package:flutter/src/material/dropdown.dart':
Failed assertion: line 805 pos 15: 'items == null || items.isEmpty || value == null ||
items.where((DropdownMenuItem<T> item) {
return item.value == value;
}).length == 1'
我尝试将 DropdownButton值设置为null 可以正常工作,但是我无法看到所选元素。FutureBuilder<List<Tag>>(
future: _tagDatabaseHelper.getTagList(),
builder: (BuildContext context, AsyncSnapshot<List<Tag>> snapshot) {
if (!snapshot.hasData) {
return Center(
child: CircularProgressIndicator(),
);
}
return ListView(
children: <Widget>[
SizedBox(
height: MediaQuery.of(context).size.height * 0.2,
),
Container(
margin: EdgeInsets.symmetric(
horizontal: MediaQuery.of(context).size.width * 0.07),
child: Theme(
data: ThemeData(canvasColor: Color(0xFF525A71)),
child: DropdownButton<Tag>(
value: _selectedTag,
isExpanded: true,
icon: Icon(
Icons.arrow_drop_down,
size: 24,
),
hint: Text(
"Select tags",
style: TextStyle(color: Color(0xFF9F9F9F)),
),
onChanged: (value) {
setState(() {
_selectedTag = value;
});
},
items: snapshot.data.map((Tag tag) {
return DropdownMenuItem<Tag>(
value: tag,
child: Text(
tag.tagTitle,
style: TextStyle(color: Colors.white),
),
);
}).toList(),
value: _selectedTag,
),
),
),
我使用 futureBuilder 来从数据库获取列表。
最佳答案
好吧,因为没有问题有完全相同的解决方案。我的代码也面临同样的问题。这是我如何解决此问题。
我的DropdownButton的代码:
DropdownButton(
items: _salutations
.map((String item) =>
DropdownMenuItem<String>(child: Text(item), value: item))
.toList(),
onChanged: (String value) {
setState(() {
print("previous ${this._salutation}");
print("selected $value");
this._salutation = value;
});
},
value: _salutation,
),
_salutation
初始化为:
String _salutation = ""; //Notice the empty String.
'items == null || items.isEmpty || value == null ||
String _salutation = "Mr."; //This is the selection value. It is also present in my array.
final _salutations = ["Mr.", "Mrs.", "Master", "Mistress"];//This is the array for dropdown
关于flutter - Flutter:应该只有[DropdownButton]值的一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60510150/
我尝试在 C# 窗口形式的 statusStrip 中创建几个 DropDownButton。这是正常的,当我将光标移动到按钮上时,它不会下拉项目,只有当我点击 DropDownButton 时它才会
希望你一切都好。我有一个小问题,我想根据另一个 DropdownButton 的值动态更改 DropdownButton 的值。 好吧,我会解释我的位置和问题所在: 首先,我有一个包含 3 个元素的
希望你一切都好。我有一个小问题,我想根据另一个 DropdownButton 的值动态更改 DropdownButton 的值。 好吧,我会解释我的位置和问题所在: 首先,我有一个包含 3 个元素的
我正在尝试实现 DropdownButton可以用标题将特定项目彼此分开(请参阅下图中的所需输出,标题为绿色) 就像在示例中一样,我想将这些绿色标题添加到我现有的 DropdownButton 中。
我将一个 DropdownButton 放在 TileList 的尾部,设置文档中显示的参数以使其处于事件状态,但无论如何它都保持禁用状态。 ListTile 是 ListView 的一部分,未包含在
我想在一行中添加一个下拉列表。 当我这样做的时候 Row( children: [ Expanded(child:TextField(
我创建了一个 dropdownButton 以允许用户从下拉列表中进行选择,该列表将通过 API 填充。现在我正在使用我创建的列表进行填充。 目前该按钮正在显示我的列表中的项目,但在做出选择后,列表不
我尝试像这样选择 DropdownButton 项: import 'package:flutter/material.dart'; import 'package:flutter_test/flut
我正在编写 Flutter Web 应用程序,并将一些小部件测试添加到我的代码库中。我很难让 flutter_test 按预期工作。我当前面临的问题是尝试在 DropdownButton 中选择一个值
例如,在 TextField 中,可以设置带有标签文本的 InputDecoration,当没有用户输入时显示在 TextField 的中心,然后显示在用户输入文本的上方。 使用 DropDownBu
在困惑中,我试图让DataTable的列之一成为DropdownButton。我希望最初从getUsers填充DataTable行,然后能够使用基于ratingList的DropdownButton编
在 flutter 中,我试图让 DataTable 的列之一成为 DropdownButton,用户可以从 DropdownButton 中进行选择,它会更改该单元格的值(而不是任何其他单元格,所以
我正在创建一个下拉列表,但是每当我单击一个值并尝试在onChanged部分中打印该值时,它都会输出null,这是我做错了什么?谢谢。这是我的代码: List _fonts = [ User
可以使用下拉按钮: return DropdownButton( items: ['Foo', 'Bar'].map((String value) { r
总结 我有一个 DropDownButton包含特定国家的所有联赛。所以本质上用户选择了 Nation来自ComboBox和 Leagues所选 Nation将添加到DropDownButton里面.
我在使用 DropdownButton 时遇到问题,我不知道为什么悬停效果比下拉菜单的实际大小长。我的 DropdownButton 位于 Telerik 网格内,这可能会导致问题。为了简单起见,我将
我想改变下拉按钮的颜色,让它看起来像“btn btn-primary”。但它也会改变所有菜单项的颜色,并将它们向右移动。这是原始下拉菜单的样子: 然后我设计成这样:
根据 this page ,DropDownButton 使用 ContextMenu 来显示 ItemsSource。我们如何知道用户点击了什么?按钮上的 Click 事件不是针对菜单的,而是针对按
我能够将提供给 DropDownButton 的 DropdownMenuItem 中使用的文本项居中,但是当菜单关闭时,如何才能将所选项目居中? 最佳答案 像这样: Dropdo
我在 appBar 和脚手架主体中创建了两个 DropdownButton。只要选择顶部 DropdownButton 就会更改语言。在我从第二个 Dropdown Button 中选择 One Co
我是一名优秀的程序员,十分优秀!