英文:
Exception has occurred. _AssertionError ('package:flutter/src/material/dropdown.dart':
问题
你好,我从浏览器中复制了代码,但在尝试时出现了错误,我该如何解决,因为我想将此代码用作学习参考,但出现了以下错误。
异常情况发生。
_AssertionError('package:flutter/src/material/dropdown.dart':断言失败:第1584行之后:'items == null || items.isEmpty || value == null ||
items.where((DropdownMenuItem<T> item) {
return item. value == value;
}).length == 1':[DropdownButton]的值应该恰好有一个项目:Dropdown 1。
检测到具有相同值的[DropdownMenuItem]为零或2个或更多)
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: DynamicDropdown(),
);
}
}
class DynamicDropdown extends StatefulWidget {
@override
_DynamicDropdownState createState() => _DynamicDropdownState();
}
class _DynamicDropdownState extends State<DynamicDropdown> {
String dropdownValue1 = 'Dropdown 1';
String dropdownValue2 = 'Dropdown 2';
List<String> dropdownItems1 = ['Item 1', 'Item 2', 'Item 3'];
List<String> dropdownItems2 = ['Item A', 'Item B', 'Item C'];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Dynamic Dropdown'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
DropdownButtonFormField(
value: dropdownValue1,
onChanged: (newValue) {
setState(() {
dropdownValue1 = newValue!;
if (dropdownValue1 == 'Item 1') {
dropdownItems2 = ['Item A', 'Item B', 'Item C'];
} else if (dropdownValue1 == 'Item 2') {
dropdownItems2 = ['Item D', 'Item E', 'Item F'];
} else {
dropdownItems2 = ['Item G', 'Item H', 'Item I'];
}
});
},
items: dropdownItems1.map((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
),
SizedBox(height: 20),
DropdownButton<String>(
value: dropdownValue2,
onChanged: (newValue) {
setState(() {
dropdownValue2 = newValue!;
});
},
items: dropdownItems2.map((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
),
],
),
),
);
}
}
英文:
Hello I copied the code from the browser when I tried it an error occurred, how do I solve it because I want to use this code as a reference in learning but the following error occurs
Exception has occurred.
_AssertionError ('package:flutter/src/material/dropdown.dart': Failed assertion: line 1584 post 15: 'items == null || items.isEmpty || value == null ||
items.where((DropdownMenuItem<T> item) {
return item. value == value;
}).length == 1': There should be exactly one item with [DropdownButton]'s value: Dropdown 1.
Either zero or 2 or more [DropdownMenuItem]s were detected with the same value)
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: DynamicDropdown(),
);
}
}
class DynamicDropdown extends StatefulWidget {
@override
_DynamicDropdownState createState() => _DynamicDropdownState();
}
class _DynamicDropdownState extends State<DynamicDropdown> {
String dropdownValue1 = 'Dropdown 1';
String dropdownValue2 = 'Dropdown 2';
List<String> dropdownItems1 = ['Item 1', 'Item 2', 'Item 3'];
List<String> dropdownItems2 = ['Item A', 'Item B', 'Item C'];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Dynamic Dropdown'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
DropdownButtonFormField(
value: dropdownValue1,
onChanged: (newValue) {
setState(() {
dropdownValue1 = newValue!;
if (dropdownValue1 == 'Item 1') {
dropdownItems2 = ['Item A', 'Item B', 'Item C'];
} else if (dropdownValue1 == 'Item 2') {
dropdownItems2 = ['Item D', 'Item E', 'Item F'];
} else {
dropdownItems2 = ['Item G', 'Item H', 'Item I'];
}
});
},
items: dropdownItems1.map((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
),
SizedBox(height: 20),
DropdownButton<String>(
value: dropdownValue2,
onChanged: (newValue) {
setState(() {
dropdownValue2 = newValue!;
});
},
items: dropdownItems2.map((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
),
],
),
),
);
}
}
答案1
得分: 1
your 'dropdownValue1' should be from 'dropdownItems1' list. error comes from here - 'value: dropdownValue1', where you have assigned item from outside the list, check below code, I have edited your code;
// String dropdownValue1 = 'Dropdown 1';
// String dropdownValue2 = 'Dropdown 2';
List
List
String dropdownValue1;
String dropdownValue2;
@override
void initState() {
// TODO: implement initState
super.initState();
dropdownValue1 = dropdownItems1[0];
dropdownValue2 = dropdownItems2[0];
}
英文:
your 'dropdownValue1' should be from 'dropdownItems1' list. error comes from here - 'value: dropdownValue1' ,where you have assigned item from outside the list, check below code, I have edited your code;
// String dropdownValue1 = 'Dropdown 1';
// String dropdownValue2 = 'Dropdown 2';
List<String> dropdownItems1 = ['Item 1', 'Item 2', 'Item 3'];
List<String> dropdownItems2 = ['Item A', 'Item B', 'Item C'];
String dropdownValue1;
String dropdownValue2;
@override
void initState() {
// TODO: implement initState
super.initState();
dropdownValue1 = dropdownItems1[0];
dropdownValue2 = dropdownItems2[0];
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论