在尝试按弹出菜单上的删除选项后,列表自动刷新。它将删除所选的 从api调用的项目列表中的项目。按下删除键后,该项目应立即消失。
使用api数据填充列表的API调用:
List data;
@override
void initState() {
super.initState();
this.getJsonData();
}
Future<String> getJsonData() async {
var response = await http.get(
Uri.encodeFull(url),
headers: {"Accept": "application/json"},
);
var extractdata = json.decode(response.body);
data = extractdata['levels'];
levelsData = data;
setState(
() {
var extractdata = json.decode(response.body);
data = extractdata['levels'];
},
);
return "Success";
}
...
Api呼叫将删除:
delete() async {
String url = "http://31.183.125.253:8080/users";
Map map = {
'price': 1.23,
'user_id': 'user2'
};
print(await apiRequest(url, map));
}
Future<String> apiRequest(String url, Map jsonMap) async {
HttpClient httpClient = new HttpClient();
HttpClientRequest request = await httpClient.deleteUrl(Uri.parse(url));
request.headers.set('content-type', 'application/json');
request.add(utf8.encode(json.encode(jsonMap)));
HttpClientResponse response = await request.close();
String statusCode = response.statusCode.toString();
String reply = await response.transform(utf8.decoder).join();
print(statusCode);
httpClient.close();
return reply;
}
存储来自api调用的列表的扩展图块:
...
var levelsData = [];
@override
Widget build(BuildContext context) {
List<dynamic> _getChildren() {
List<Widget> children = [];
levelsData.forEach(
(element) {
children.add(
ListTile(
title: Text(element['price'].toString(),
trailing: PopUpMenu(
onDelete: () {
setState(() {
delete();
});
}))
);}
);
return children;
}
}
...
弹出菜单:
class PopUpMenu extends StatelessWidget {
VoidCallback onDelete;
PopUpMenu({this.onDelete});
void showMenuSelection(String value) {
switch (value) {
case 'Delete':
onDelete();
break;
// Other cases for other menu options
}
}
我能够删除选择要删除的项目,但是该项目不会从列表中消失,并且也不会显示列表的更改。
答案 0 :(得分:1)
Flutter应该自动更新以反映对数据源的更改。但是,这仅在调用后应用:
setState((){
});
因此应在从列表中删除该项目后运行。
答案 1 :(得分:1)
您必须调用setState并将其从列表中删除。
<div class="form-group">
<label for="file" class="form__label">Imagem de Perfil</label>
<input type="file" class="form-control-file" id="files" name="images" accept="image/x-png,image/gif,image/jpeg" />
</div>