总体目标是使用异步getpays函数在下拉菜单中提供项目列表 如何在小部件中使用手势检测器以及如何设置状态下拉菜单?我想用它来调用我的getpays函数,但是当我将其插入到dropdow小部件下时,出现语法错误“发现期望的正电子参数过多0。1。我使用MVC模式。 我的屏幕
Widget build(BuildContext context) {
return new Scaffold(
body: Container(
width: double.infinity,
height: double.infinity,
child: new Form(
child: Padding(
padding: const EdgeInsets.only(top: 16.0, left: 28),
child: new Column(children: <Widget>[
DropdownButtonFormField(
GestureDetector(onTap: () async {
await moncontrolleurdropdown.getpays();
setState(() {});
}),
hint: Text("Select"),
items: moncontrolleurdropdown.myPays
?.map((PaysModel pays) {
DropdownMenuItem<String>(
child: new Text(pays.nompays),
value: pays.codestate,
);
})?.toList(),
onChanged: (value) =>
moncontrolleurdropdown.onSelectedState(value),
value: moncontrolleurdropdown.selectedState,
),
DropdownButtonFormField(
items: moncontrolleurdropdown.tempvillesList
?.map((VilleModel ville) {
return new DropdownMenuItem<String>(
child: new Text(ville.nomville),
value: ville.codeville,
);
})?.toList(),
onChanged: (value) =>
moncontrolleurdropdown.onSelectedVille(value),
value: moncontrolleurdropdown.selectedVille,
),
])))));
}
}
Widget build(BuildContext context) {
return new Scaffold(
body: Container(
width: double.infinity,
height: double.infinity,
child: new Form(
child: Padding(
padding: const EdgeInsets.only(top: 16.0, left: 28),
child: new Column(children: <Widget>[
DropdownButtonFormField(
GestureDetector(onTap: () async {
await moncontrolleurdropdown.getpays();
setState(() {});
}),
hint: Text("Select"),
items: moncontrolleurdropdown.myPays
?.map((PaysModel pays) {
DropdownMenuItem<String>(
child: new Text(pays.nompays),
value: pays.codestate,
);
})?.toList(),
onChanged: (value) =>
moncontrolleurdropdown.onSelectedState(value),
value: moncontrolleurdropdown.selectedState,
),
DropdownButtonFormField(
items: moncontrolleurdropdown.tempvillesList
?.map((VilleModel ville) {
return new DropdownMenuItem<String>(
child: new Text(ville.nomville),
value: ville.codeville,
);
})?.toList(),
onChanged: (value) =>
moncontrolleurdropdown.onSelectedVille(value),
value: moncontrolleurdropdown.selectedVille,
),
])))));
}
}
我的函数getpays和onselecstate
void getpays() async {
try {
var response =
await http.get("http://..../getpays.php");
if (200 == response.statusCode) {
print("testfonction");
print(response.body);
// setState(() {
_myPays = (json.decode(response.body) as List)
.map((i) => PaysModel.fromJson(i))
.toList();
// print(_myPays.length);
print(_myPays);
_myPaystest = _myPays;
//});
}
} catch (e) {
//return _myPays;
}
//return _myPays;
}
<code>
void onSelectedState(String value) {
this.getville();
print(value);
print("a");
print(this.villesList.length);
print("b");
//selecteddState=value;
print("c");
//print(_selectedState);
this.tempvillesList.clear();
for (var x = 0; x == this.villesList.length - 1; x++) {
print(x);
if (this.villesList[x].codepays == _selectedState) {
print(this.villesList[x].codepays);
this.tempvillesList.add(this.villesList[x]);
print(this.tempvillesList);
}
}
}