Я новичок Чтобы развязать развитие и хочу отобразить три выпадающих списка, зависящих друг от друга, например сначала штат, затем город, а затем район. И данные для этих выпадающих меню поступают из API остальных в формате json, например [{"area": "ABC", "areaID": "3099"}, {"area": "Test1", "areaID": "3100 "}, {" area ":" test2 "," areaID ":" 3101 "}] Аналогично для штата и города
Вот мой код:
import 'package:date_format/date_format.dart';
import 'package:flutter/material.dart';
import 'package:project_rapl/Utils/Values.dart';
import 'package:project_rapl/services/AddNewTask.dart';
import 'package:project_rapl/services/AreaList.dart';
import 'package:project_rapl/services/CityList.dart';
import 'package:project_rapl/services/MyDatePicker.dart';
import 'package:project_rapl/services/StatesList.dart';
class AddTask extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Add New Task"),
),
body: AddTaskBody(),
);
}
}
class AddTaskBody extends StatefulWidget {
@override
_AddTaskBodyState createState() => _AddTaskBodyState();
}
class _AddTaskBodyState extends State<AddTaskBody> {
StatesList s = StatesList();
List _states = List();
List _city = List();
List _area = List();
String date,activityDate,docType,docName,mobile,email,state,city,area,pincode,
gstNo,address,purposeVisit,visitStatus,remark,mrName;
MyDatePicker datePicker = MyDatePicker();
TextEditingController dateController = TextEditingController();
getStates()async{
List st = await s.fetchStates();
setState(() {
_states = st;
});
}
getCity()async{
}
@override
void initState() {
// TODO: implement initState
super.initState();
this.getStates();
}
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
child: Container(
padding: EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Padding(
padding: EdgeInsets.symmetric(horizontal: 5.0,vertical: 5.0),
child: TextField(
controller: dateController,
readOnly: true,
autocorrect: false,
autofocus: false,
style: TextStyle(
fontWeight: FontWeight.w400,
color: Colors.black
),
decoration: InputDecoration(
hintText: "Select Date",
hintStyle: TextStyle(fontFamily: "OpenSans")
),
onTap: ()async{
DateTime myDate = await datePicker.pickDate(context);
setState(() {
date = formatDate(myDate, [yyyy, '-', mm, '-', dd]);
dateController.text = date;
});
},
),
),
Padding(
padding: EdgeInsets.symmetric(horizontal:5.0,vertical:5.0),
child: DropdownButton(
items: _states.map((items){
return DropdownMenuItem(
value: items["id"].toString(),
child: Text(items["state"]),
);
}).toList(),
onChanged: (newVal)async{
CityList c = CityList(newVal);
List ci = await c.fetchCity();
setState(() {
state = newVal;
_city = ci;
if(_city.isEmpty){
_area = [];
}
});
},
value: state,
isExpanded: true,
underline: Container(
color: Values().grey,
height: 1.0,
),
iconDisabledColor: Values().grey,
iconEnabledColor: Values().primaryColor,
hint: Text(
"Select State",
style: TextStyle(fontFamily: "OpenSans"),
),
),
),
Padding(
padding: EdgeInsets.symmetric(horizontal: 5.0,vertical: 5.0),
child: DropdownButton(
items: _city.map((items){
return DropdownMenuItem(
value: items["cityID"].toString(),
child: Text(items["city"]),
);
}).toList(),
onChanged: (newVal)async{
AreaList c = AreaList(newVal);
List ar = await c.fetchArea();
setState(() {
city = newVal;
_area = ar;
});
},
value: city,
isExpanded: true,
underline: Container(
color: Values().grey,
height: 1.0,
),
iconDisabledColor: Values().grey,
iconEnabledColor: Values().primaryColor,
hint: Text(
"Select City",
style: TextStyle(fontFamily: "OpenSans"),
),
),
),
Padding(
padding: EdgeInsets.symmetric(horizontal: 5.0,vertical: 5.0),
child: DropdownButton(
items: _area.map((items){
return DropdownMenuItem(
value: items["areaID"].toString(),
child: Text(items["area"]),
);
}).toList(),
onChanged: (newVal)async{
setState(() {
area = newVal;
});
},
value: _city.isEmpty ? null : area,
isExpanded: true,
underline: Container(
color: Values().grey,
height: 1.0,
),
iconDisabledColor: Values().grey,
iconEnabledColor: Values().primaryColor,
hint: Text(
"Select City",
style: TextStyle(fontFamily: "OpenSans"),
),
),
),
Padding(
padding: EdgeInsets.symmetric(horizontal: 5.0,vertical: 5.0),
child: TextField(),
),
Padding(
padding: EdgeInsets.symmetric(horizontal: 5.0,vertical: 5.0),
child: TextField(),
),
Padding(
padding: EdgeInsets.symmetric(horizontal: 5.0,vertical: 5.0),
child: TextField(),
),
Padding(
padding: EdgeInsets.symmetric(horizontal: 5.0,vertical: 5.0),
child: TextField(),
),
Padding(
padding: EdgeInsets.symmetric(horizontal: 5.0,vertical: 5.0),
child: TextField(),
),
Padding(
padding: EdgeInsets.symmetric(horizontal: 5.0,vertical: 5.0),
child: TextField(),
),
Padding(
padding: EdgeInsets.symmetric(horizontal: 5.0,vertical: 5.0),
child: TextField(),
),
Padding(
padding: EdgeInsets.symmetric(horizontal: 5.0,vertical: 5.0),
child: TextField(),
),
Padding(
padding: EdgeInsets.symmetric(horizontal: 5.0,vertical: 5.0),
child: TextField(),
),
Padding(
padding: EdgeInsets.symmetric(horizontal: 5.0,vertical: 5.0),
child: TextField(),
),
Padding(
padding: EdgeInsets.symmetric(horizontal: 5.0,vertical: 5.0),
child: TextField(),
),
Padding(
padding: EdgeInsets.symmetric(horizontal: 5.0,vertical: 5.0),
child: TextField(),
),
Container(
width: MediaQuery.of(context).size.width,
padding: EdgeInsets.symmetric(horizontal: 25.0,vertical: 15.0),
child: RaisedButton(
padding: EdgeInsets.all(12.0),
color: Values().primaryColor,
onPressed: AddNewTask(date, activityDate, docType, docName,
mobile, email, state, city, area, pincode, gstNo, address,
purposeVisit, visitStatus, remark, mrName).addTask,
child: Text(
"ADD TASK",
style: TextStyle(color: Colors.white,fontFamily: "OpenSans",fontSize: 15.0,fontWeight: FontWeight.bold),
),
),
),
],
),
),
);
}
}
ЭтоОшибка, которую я получаю;
════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following assertion was thrown building AddTaskBody(dirty, dependencies: [MediaQuery], state: _AddTaskBodyState#bf72c):
'package:flutter/src/material/dropdown.dart': Failed assertion: line 620 pos 15: 'items == null || items.isEmpty || value == null || items.where((DropdownMenuItem<T> item) => item.value == value).length == 1': is not true.
Either the assertion indicates an error in the framework itself, or we should provide substantially more information in this error message to help you determine and fix the underlying cause.
In either case, please report this assertion by filing a bug on GitHub:
https://github.com/flutter/flutter/issues/new?template=BUG.md
User-created ancestor of the error-causing widget was:
Scaffold file:///M:/Projects/MyProjects/Apps/Flutter/project_rapl/lib/Screens/TaskScreens/AddTaskUI.dart:12:12
When the exception was thrown, this was the stack:
#2 new DropdownButton (package:flutter/src/material/dropdown.dart:620:15)
#3 _AddTaskBodyState.build (package:project_rapl/Screens/TaskScreens/AddTaskUI.dart:118:22)
#4 StatefulElement.build (package:flutter/src/widgets/framework.dart:4047:27)
#5 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3941:15)
#6 Element.rebuild (package:flutter/src/widgets/framework.dart:3738:5)