Flutter:应该只有一项具有 [DropdownButton] 的值

2023-11-25

我正在尝试创建一个下拉按钮在颤振中。我得到了列表来自我的数据库然后我将列表传递给我的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,
              ),
            ),
          ),

I used 未来建造者 to 从数据库获取我的列表.


好吧,因为没有问题有完全相同的解决方案。我的代码也面临同样的问题。这是我解决这个问题的方法。

我的 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 as:

String _salutation = ""; //Notice the empty String.

这是一个错误!

正如错误消息正确提到的那样,初始选择不应为 null 或为空。

'项目==空|| items.isEmpty ||值==空||

因此崩溃了:

crash_message

Solution
使用一些默认值初始化值对象。请注意value 应该是您的集合中包含的值之一。如果不是,那么就会发生崩溃。

  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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Flutter:应该只有一项具有 [DropdownButton] 的值 的相关文章

随机推荐