Flutter:下拉问题
Flutter : Dropdown Issues
只要我 select 从我的下拉列表中选择一个值,就会出现此错误
'package: flutter/src/material/dropdown.dart/: Failed assertion: line 855 pos 15: 'item == bull || item.isEmpty || value == null|| item.where((DropdownMenuItemitem){ retuen item.value == value;)}.length ==1': There should be exactly a item with [DropdownButton]'s value: Category 1. 要么为零或检测到 2 个或更多 [DropdownMenuItem] 具有相同的值
如何纠正?
这是我的代码。这将是一个很大的帮助。
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
void main() {
runApp(MaterialApp(home: MyApp()));
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String valueChoose;
List _listItem = ["Category 1", "Category 2", "Category 3", "Category 4"];
List _listItem1 = [
"Sub Category 1",
"Sub Category 2",
"Sub Category 3",
"Sub Category 4"
];
List _listItem2 = ["CRIS", "ADMINISTRATION", "ZONE", "DEPARTMENT"];
var selectedState;
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Feedback"),
centerTitle: true,
leading: IconButton(
onPressed: () {},
icon: Icon(Icons.home),
),
),
body: Container(
child: Center(
child: Form(
autovalidateMode: AutovalidateMode.onUserInteraction,
key: _formKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text("Category"),
DropdownButton(
hint: Text("Select"),
items: _listItem
.map<DropdownMenuItem<String>>((valueItem) {
return DropdownMenuItem<String>(
value: valueItem,
child: Text(valueItem),
);
}).toList(),
onChanged: (selectedAccountType) {
setState(() {
selectedState = selectedAccountType;
});
},
value: selectedState,
)
],
),
SizedBox(
height: 10,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text("SUBCATEGORY"),
DropdownButton(
hint: Text("Select"),
items: _listItem1
.map<DropdownMenuItem<String>>((valueItem1) {
return DropdownMenuItem<String>(
value: valueItem1,
child: Text(valueItem1),
);
}).toList(),
onChanged: (selectedAccountType) {
setState(() {
selectedState = selectedAccountType;
});
},
value: selectedState,
)
],
),
SizedBox(height: 10),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text("MARKED TO"),
DropdownButton(
hint: Text("Select"),
items: _listItem2
.map<DropdownMenuItem<String>>((valueItem2) {
return DropdownMenuItem<String>(
value: valueItem2,
child: Text(valueItem2),
);
}).toList(),
onChanged: (selectedAccountType) {
setState(() {
selectedState = selectedAccountType;
});
},
value: selectedState,
)
],
),
SizedBox(height: 10),
Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Padding(
padding: EdgeInsets.fromLTRB(70, 00, 70, 00),
child: TextField(
decoration: InputDecoration(
hintText: "Describe your problem here.",
),
maxLength: 1000,
maxLines: 5,
),
)
],
),
SizedBox(height: 10),
ButtonTheme(
child: ElevatedButton(
child: Text("Submit"),
onPressed: () {},
style: ElevatedButton.styleFrom(
padding: EdgeInsets.symmetric(
horizontal: 25, vertical: 15),
),
),
),
]),
),
),
));
}
}
第一个问题是您正在为所有 DropdownMenu
小部件使用 selectedState
变量。这是不切实际的,它可能会导致意外的行为和错误。您应该为每个 DropdownMenu
定义一个单独的变量。
另一个问题是您初始化 selectedState
变量的方式。
您正在这样做:var selectedState;
,但没有指定值,因此默认情况下它的值为 null
。
但是,selectedState
变量的初始值必须是 DropdownMenu
.
的选项之一
例如,如果这些 List<String> options = <String>["Option 1", "Option 2", "Option 3", "Option 4"];
是 DropdownMenu
的选项,那么您必须像这样初始化 selectedState
变量,例如:var selectedState = "Option 1";
。
错误消失了!
只要我 select 从我的下拉列表中选择一个值,就会出现此错误
'package: flutter/src/material/dropdown.dart/: Failed assertion: line 855 pos 15: 'item == bull || item.isEmpty || value == null|| item.where((DropdownMenuItemitem){ retuen item.value == value;)}.length ==1': There should be exactly a item with [DropdownButton]'s value: Category 1. 要么为零或检测到 2 个或更多 [DropdownMenuItem] 具有相同的值
如何纠正?
这是我的代码。这将是一个很大的帮助。
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
void main() {
runApp(MaterialApp(home: MyApp()));
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String valueChoose;
List _listItem = ["Category 1", "Category 2", "Category 3", "Category 4"];
List _listItem1 = [
"Sub Category 1",
"Sub Category 2",
"Sub Category 3",
"Sub Category 4"
];
List _listItem2 = ["CRIS", "ADMINISTRATION", "ZONE", "DEPARTMENT"];
var selectedState;
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Feedback"),
centerTitle: true,
leading: IconButton(
onPressed: () {},
icon: Icon(Icons.home),
),
),
body: Container(
child: Center(
child: Form(
autovalidateMode: AutovalidateMode.onUserInteraction,
key: _formKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text("Category"),
DropdownButton(
hint: Text("Select"),
items: _listItem
.map<DropdownMenuItem<String>>((valueItem) {
return DropdownMenuItem<String>(
value: valueItem,
child: Text(valueItem),
);
}).toList(),
onChanged: (selectedAccountType) {
setState(() {
selectedState = selectedAccountType;
});
},
value: selectedState,
)
],
),
SizedBox(
height: 10,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text("SUBCATEGORY"),
DropdownButton(
hint: Text("Select"),
items: _listItem1
.map<DropdownMenuItem<String>>((valueItem1) {
return DropdownMenuItem<String>(
value: valueItem1,
child: Text(valueItem1),
);
}).toList(),
onChanged: (selectedAccountType) {
setState(() {
selectedState = selectedAccountType;
});
},
value: selectedState,
)
],
),
SizedBox(height: 10),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text("MARKED TO"),
DropdownButton(
hint: Text("Select"),
items: _listItem2
.map<DropdownMenuItem<String>>((valueItem2) {
return DropdownMenuItem<String>(
value: valueItem2,
child: Text(valueItem2),
);
}).toList(),
onChanged: (selectedAccountType) {
setState(() {
selectedState = selectedAccountType;
});
},
value: selectedState,
)
],
),
SizedBox(height: 10),
Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Padding(
padding: EdgeInsets.fromLTRB(70, 00, 70, 00),
child: TextField(
decoration: InputDecoration(
hintText: "Describe your problem here.",
),
maxLength: 1000,
maxLines: 5,
),
)
],
),
SizedBox(height: 10),
ButtonTheme(
child: ElevatedButton(
child: Text("Submit"),
onPressed: () {},
style: ElevatedButton.styleFrom(
padding: EdgeInsets.symmetric(
horizontal: 25, vertical: 15),
),
),
),
]),
),
),
));
}
}
第一个问题是您正在为所有 DropdownMenu
小部件使用 selectedState
变量。这是不切实际的,它可能会导致意外的行为和错误。您应该为每个 DropdownMenu
定义一个单独的变量。
另一个问题是您初始化 selectedState
变量的方式。
您正在这样做:var selectedState;
,但没有指定值,因此默认情况下它的值为 null
。
但是,selectedState
变量的初始值必须是 DropdownMenu
.
的选项之一
例如,如果这些 List<String> options = <String>["Option 1", "Option 2", "Option 3", "Option 4"];
是 DropdownMenu
的选项,那么您必须像这样初始化 selectedState
变量,例如:var selectedState = "Option 1";
。
错误消失了!