【问题标题】:Changing Two Level DropdownButtonFormField : There should be exactly one item with [DropdownButton]'s value更改两级 DropdownButtonFormField :应该只有一项具有 [DropdownButton] 的值
【发布时间】:2020-08-26 10:34:20
【问题描述】:

尽管这里有多个条目似乎有类似的问题,但我无法让它真正发挥作用。

我有两个依赖的 DropdownButtonFormFields 的设置,其中第二个在第一个更改后更改为另一个列表。

  1. 我能够将问题分解为第二个选择的选定值的持续剩余。我希望它会随着我在代码中提供的值信息而改变。

提供以下错误

════════ Exception caught by widgets library ═══════════════════════════════════

There should be exactly one item with [DropdownButton]'s value: GreenBananas. 

Either zero or 2 or more [DropdownMenuItem]s were detected with the same value
'package:flutter/src/material/dropdown.dart':
Failed assertion: line 827 pos 15: 'items == null || items.isEmpty || value == null ||
              items.where((DropdownMenuItem<T> item) {
                return item.value == value;
              }).length == 1'
The relevant error-causing widget was
    DropdownButtonFormField<String> 
lib/…/testing/test.dart:242
══════════════════════════════════════════════════════════════════

我简化了示例并重建了错误,以便更好地分析问题并从你们那里获得更有价值的意见:)

class InputRowTest extends StatefulWidget {
  @override
  _InputRowTestState createState() => _InputRowTestState();
}

class _InputRowTestState extends State<InputRowTest> {
  List<String> list1 = ['Apples', 'Bananas', 'Peaches'];

  List<String> list1_1 = ['GreenApples', 'RedApples', 'YellowApples'];

  List<String> list1_2 = [
    'YellowBananas',
    'BrownBananas',
    'GreenBananas',
    'GreenApples'
  ];

  List<String> list1_3 = [
    'RedPeaches',
    'YellowPeaches',
    'GreenPeaches',
    'GreenApples'
  ];

  List<String> _fromparent;
  int _fromparentint;
  //String selected;

  @override
  void initState() {
    _fromparent = list1_1;
    _fromparentint = 0;
    //selected = list1[0];
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    List<List<String>> subLists = [list1_1, list1_2, list1_3];
    _fromparent = subLists[_fromparentint];

    DropdownButtonFormField ddff = DropdownButtonFormField(
      //value: selected, //list1[0],
      //items: list1.map((category) {
      value: _fromparent[0], //Seems this value wont change.
      items: _fromparent.map((category) {
        return DropdownMenuItem(
          value: category,
          child: Container(
            child: Text(category),
          ),
        );
      }).toList(),
      onChanged: (val) => print(val),
    );

    return Center(
      child: Row(
        children: <Widget>[
          Expanded(
            child: DropdownButtonFormField(
              value: list1[0],
              items: list1.map((category) {
                return DropdownMenuItem(
                  value: category,
                  child: Container(
                    child: Text(category),
                  ),
                );
              }).toList(),
              onChanged: (val) {
                setState(() {
                  //selected = val;
                  _fromparentint = list1.indexOf(val);
                });
              },
            ),
          ),
          Expanded(
            child: ddff,
          ),
        ],
      ),
    );
  }
}

【问题讨论】:

    标签: forms flutter dropdown


    【解决方案1】:

    我找到了解决此问题的方法。

    Flutter 似乎没有重建 DropDownFormField,而是认为保留它是完全可以的。在这种情况下,它甚至还很固执。

    由于我找不到重建 Field 的方法,所以我创建了一个非常讨厌但工作的方法。还需要一些润色。

    我基本上让 Flutter 相信我每次都提供不同的小部件。

    class InputRowTest extends StatefulWidget {
      @override
      _InputRowTestState createState() => _InputRowTestState();
    }
    
    class _InputRowTestState extends State<InputRowTest> {
      List<String> list1 = ['Apples', 'Bananas', 'Peaches'];
    
      List<String> list1_1 = ['GreenApples', 'RedApples', 'YellowApples'];
    
      List<String> list1_2 = [
        'YellowBananas',
        'BrownBananas',
        'GreenBananas',
        'GreenApples'
      ];
    
      List<String> list1_3 = [
        'RedPeaches',
        'YellowPeaches',
        'GreenPeaches',
        'GreenApples'
      ];
    
      List<String> _fromparent;
      int _fromparentint;
      Widget ddbff;
      var selected;
      bool chance;
    
      Widget ddff(List<String> list, bool chance) {
        return (chance)
            ? DropdownButtonFormField(
                value: list[0], //Seems this value wont change.
                items: list.map((category) {
                  return DropdownMenuItem(
                    value: category,
                    child: Container(
                      child: Text(category),
                    ),
                  );
                }).toList(),
                onChanged: (val) {
                  print(val);
                },
              )
            : Container(
                child: DropdownButtonFormField(
                  value: list[0], //Seems this value wont change.
                  items: list.map((category) {
                    return DropdownMenuItem(
                      value: category,
                      child: Container(
                        child: Text(category),
                      ),
                    );
                  }).toList(),
                  onChanged: (val) {
                    print(val);
                  },
                ),
              );
      }
    
      @override
      void initState() {
        _fromparent = list1_1;
        _fromparentint = 0;
        selected = list1_1[0];
        chance = true;
        ddbff = ddff(_fromparent, chance);
        super.initState();
      }
    
      @override
      Widget build(BuildContext context) {
        List<List<String>> subLists = [list1_1, list1_2, list1_3];
        _fromparent = subLists[_fromparentint];
    
        chance = !chance;
        ddbff = ddff(_fromparent, chance);
    
        return Center(
          child: Container(
            child: Row(
              children: <Widget>[
                Expanded(
                  child: DropdownButtonFormField(
                    value: list1[0],
                    items: list1.map((category) {
                      return DropdownMenuItem(
                        value: category,
                        child: Container(
                          child: Text(category),
                        ),
                      );
                    }).toList(),
                    onChanged: (val) {
                      setState(() {
                        _fromparentint = list1.indexOf(val);
                      });
                    },
                  ),
                ),
                Expanded(
                  child: ddbff,
                ),
              ],
            ),
          ),
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-05-01
      • 1970-01-01
      • 2023-01-17
      • 2021-08-29
      • 2020-06-16
      • 1970-01-01
      • 2020-07-18
      • 2021-12-03
      • 2021-11-05
      相关资源
      最近更新 更多