gpt4 book ai didi

flutter - 在DropDownMenuItem中选择特定项目时出错

转载 作者:行者123 更新时间:2023-12-03 04:00:11 26 4
gpt4 key购买 nike

我有一个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) Exception caught by widgets library ═══════════════════════════════════════════════════ 'package:flutter/src/material/dropdown.dart': Failed assertion: line 620 pos 15: 'items == null || items.isEmpty || value == null || items.where((DropdownMenuItem item) => item.value == value).length == 1': is not true. User-created ancestor of the error-causing widget was:

最佳答案

DropdownButtonFormField的项必须唯一,偶然地在“frequency”列表中有两个“Thrice a week”字符串。

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,
),
);
}
}

关于flutter - 在DropDownMenuItem中选择特定项目时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57884417/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com