在DropDownMenuItem

时间:2019-09-11 07:45:59

标签: flutter dart flutter-layout

我有一个dropDownFormField,其中有一个doprDownButton项。除了下拉菜单中的“每周三次”之外,下拉菜单中的所有其他项目均有效。

尝试在设置下拉列表的值之前先设置条件

Expanded(
  flex: 1,
  child: Container(
    width: 180.0,
    child: DropdownButtonFormField(
      decoration: InputDecoration(
          filled: true,
          labelText: "Frequency",
          border: OutlineInputBorder(),
          fillColor: Colors.black12),
      validator: (val) {
        if (val == null ) {
          return "Select the units";
        } else {
          return null;
        }
      },
      items: dummyData.frequency
          .map((value) => DropdownMenuItem(
                child: Text(
                  value,
                ),
                value: value,
              ))
          .toList(),
      onChanged: (selectedFrequency) {
        setState(() {
          selectedFrequencyItem = selectedFrequency;
        });
      },
      value: selectedFrequencyItem != null ? selectedFrequencyItem : null,
    ),
  ),
),
  

════════(2)小部件库捕获到异常   ══════════════════════════════════════════════════ ═   'package:flutter / src / material / dropdown.dart':断言失败:行   620 pos 15:“项目== null || items.isEmpty ||值== null ||   items.where(((DropdownMenuItem item)=> item.value == value).length   == 1':不正确。用户创建的导致错误的小部件的祖先是:

1 个答案:

答案 0 :(得分:0)

DropdownButtonFormField的项必须唯一,偶然地在“频率”列表中有两个“每周三次”字符串。

enter image description here

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: Scaffold(
        body: SafeArea(
          child: MyHomePage(),
        ),
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  List<String> frequency = [
    'Immediately',
    'Once a day',
    "Twice a day",
    "Thrice a day",
    "Four times a day",
    "Every 2 hours",
    "Every 3 hours",
    "Every 4 hours",
    "Every 6 hours",
    "Every 8 hours",
    "Every 12 hours",
    "On alternative days",
    "Twice a week",
    "Thrice a week",
    "Every 2 weeks",
    "Every 3 weeks",
    "Once a month",
    "Five times a day",
    "Four days a week",
    "Five days a week",
    "Six days a week",
  ];

  String selectedFrequencyItem;

  @override
  Widget build(BuildContext context) {
    return Container(
      width: 180,
      child: DropdownButtonFormField(
        decoration: InputDecoration(
          filled: true,
          labelText: "Frequency",
          border: OutlineInputBorder(),
          fillColor: Colors.black12,
        ),
        items: frequency
            .map((value) => DropdownMenuItem(
                  child: Text(
                    value,
                    style: TextStyle(fontSize: 12),
                  ),
                  value: value,
                ))
            .toList(),
        onChanged: (selectedFrequency) {
          setState(() {
            selectedFrequencyItem = selectedFrequency;
          });
        },
        value: selectedFrequencyItem,
      ),
    );
  }
}