Flutter 颤振:下拉问题

Flutter 颤振:下拉问题,flutter,drop-down-menu,dropdown,flutter-dependencies,flutter-test,Flutter,Drop Down Menu,Dropdown,Flutter Dependencies,Flutter Test,一旦我从下拉列表中选择了一个值,就会出现此错误 'package:flatter/src/material/dropdown.dart/:失败的断言:第855行位置15:'item==bull | | | item.isEmpty | | | value==null | | | item.where((dropdownMenuitem){retuen item.value==value;)}.length==1':应该只有一个具有[DropdownButton]值的项:Category 1。检测

一旦我从下拉列表中选择了一个值,就会出现此错误

'package:flatter/src/material/dropdown.dart/:失败的断言:第855行位置15:'item==bull | | | item.isEmpty | | | value==null | | | item.where((dropdownMenuitem){retuen item.value==value;)}.length==1':应该只有一个具有[DropdownButton]值的项: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),
                    ),
                  ),
                ),
              ]),
        ),
      ),
    ));
   }
   }
导入“包装:颤振/材料.省道”;
进口“包装:颤振/cupertino.dart”;
void main(){
runApp(MaterialApp(home:MyApp());
}
类MyApp扩展了StatefulWidget{
@凌驾
_MyAppState createState()=>\u MyAppState();
}
类MyAppState扩展了状态{
字符串值选择;
列表_listItem=[“第1类”、“第2类”、“第3类”、“第4类”];
列表_listItem1=[
“第1小类”,
“第2小类”,
“第3小类”,
“第4小类”
];
列表_listItem2=[“CRIS”、“管理”、“区域”、“部门”];
变量选择状态;
最终的GlobalKey _formKey=GlobalKey();
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(“反馈”),
标题:对,
领先:IconButton(
按下:(){},
图标:图标(Icons.home),
),
),
主体:容器(
儿童:中心(
孩子:表格(
autovalidateMode:autovalidateMode.onUserInteraction,
键:_formKey,
子:列(
mainAxisAlignment:mainAxisAlignment.center,
crossAxisAlignment:crossAxisAlignment.center,
儿童:[
划船(
mainAxisAlignment:mainAxisAlignment.space,
crossAxisAlignment:crossAxisAlignment.center,
儿童:[
文本(“类别”),
下拉按钮(
提示:文本(“选择”),
项目:\u列表项目
.map((valueItem){
返回下拉菜单项(
值:valueItem,
子项:文本(valueItem),
);
}).toList(),
一旦更改:(已选择AccountType){
设置状态(){
selectedState=selectedAccountType;
});
},
值:selectedState,
)
],
),
大小盒子(
身高:10,
),
划船(
mainAxisAlignment:mainAxisAlignment.space,
crossAxisAlignment:crossAxisAlignment.center,
儿童:[
文本(“子类别”),
下拉按钮(
提示:文本(“选择”),
项目:\列表项目1
.map((valueItem1){
返回下拉菜单项(
值:valueItem1,
子项:文本(valueItem1),
);
}).toList(),
一旦更改:(已选择AccountType){
设置状态(){
selectedState=selectedAccountType;
});
},
值:selectedState,
)
],
),
尺寸箱(高度:10),
划船(
mainAxisAlignment:mainAxisAlignment.space,
crossAxisAlignment:crossAxisAlignment.center,
儿童:[
文本(“标记为”),
下拉按钮(
提示:文本(“选择”),
项目:\列表项目2
.map((valueItem2){
返回下拉菜单项(
值:valueItem2,
子项:文本(valueItem2),
);
}).toList(),
一旦更改:(已选择AccountType){
设置状态(){
selectedState=selectedAccountType;
});
},
值:selectedState,
)
],
),
尺寸箱(高度:10),
纵队(
mainAxisAlignment:mainAxisAlignment.center,
crossAxisAlignment:crossAxisAlignment.center,
儿童:[
填充物(
填充:从LTRB(70,00,70,00)开始的边缘设置,
孩子:TextField(
装饰:输入装饰(
hintText:“在这里描述您的问题。”,
),
最大长度:1000,
最大行数:5,
),
)
],
),
尺寸箱(高度:10),
钮扣(
儿童:升降按钮(
儿童:文本(“提交”),
按下:(){},
样式:ElevatedButton.styleFrom(
填充:EdgeInsets.symmetric(
水平:25,垂直:15),
),
),
),
]),
),
),
));
}
}

第一个问题是,您正在为所有
下拉菜单
小部件使用
selectedState
变量。这是不实际的,它可能会导致意外的行为和错误。您应该为每个
下拉菜单定义一个单独的变量。


另一个问题是w