Flutter:应该只有一项具有 [DropdownButton] 的值 [英] Flutter: There should be exactly one item with [DropdownButton]'s value

查看:42
本文介绍了Flutter:应该只有一项具有 [DropdownButton] 的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Flutter 中创建一个下拉按钮.我从我的数据库中获取一个 列表 然后我将列表传递给我的 dropdownButton 一切正常 数据按预期显示,但是 当我从中选择一个元素我收到这个错误:

I am trying to create a dropdown button in Flutter. I am getting a List from my database then I pass the list to my dropdownButton everything works the data is shown as intended but when I choose an element from it I get this error:

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 它可以工作,但随后我看不到所选元素.

I tried setting DropdownButton value to null it works but then I can't see the chosen element.

这是我的代码:

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从数据库中获取我的列表.

推荐答案

好吧,既然没有问题有完全相同的解决方案.我的代码面临同样的问题.这是我解决这个问题的方法.

Well, since no problem has an exact same solution. I was facing the same issue with my code. Here is How I fixed this.

我的下拉按钮代码:

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 初始化为:

In the code snippet below, I am setting the state for a selection value, which is of type String. Now problem with my code was the default initialization of this selection value. Initially, I was initializing the variable _salutation as:

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

这是一个错误!

初始选择不应为 null 或为空,因为错误消息正确提及.

'items == null ||items.isEmpty ||值 == 空 ||

'items == null || items.isEmpty || value == null ||

因此崩溃:

解决方案
使用一些默认值初始化值对象.请注意值应该是您的集合中包含的值之一.如果不是,那么预计会发生崩溃.

Solution
Initialize the value object with some default value. Please note that the value should be the one of the values contained by your collection. If it is not, then expect a crash.

  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:应该只有一项具有 [DropdownButton] 的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆