Reputation: 2748
How can I refresh a ListView
?
Let say in page A, I have a ListView
, and there is a menu icon in the row item.
When I click the menu icon, it will show a bottom sheet dialog which has a delete icon.
When delete icon is clicked, it will pop up a delete confirmation dialog.
Once the 'Yes' button in confirmation dialog is clicked, it will delete the item. Once it received the "Success" status, it will refresh the ListView
.
This is the code for bottom sheet delete icon
onTap: () {
Navigator.pop(context);
var result = PopUpDialog().showDeleteDialog(); // pop up confirmation dialog
if (result == 'Success') {
print('success');
setState(() {
data.removeAt(index);
});
} else {
print('fjeodpedp');
}
},
And this is the code for Yes
button in confirmation dialog.
PopUpDialog-showDeleteDialog
onPressed: () async {
Navigator.pop(buildContext); // dismiss confirmation dialog
var result = await _bloc.delete();
return result;
},
Bloc class
Future delete() async {
Response delete = await _repo.delete(); // delete data in server
var deleteResponse = Response.fromJson(delete.body);
return deleteResponse.status; // return Success
}
I want the setState get called only if deleteResponse.status is equal to success, but it keep printing fjeodpedp
once the confirmation dialog is pop up. I have added async-await, but still not working.
What is the correct way?
Thanks for your valuable time.
Upvotes: 4
Views: 12564
Reputation: 1086
I think you're not waiting for your dialog confirmation. You should use await method to wait for the result of that dialog. I don't know what you wrote in
PopUpDialog().showDeleteDialog();
method but it should be async to perform the after operations.
For now you can write this statement to resolve the error.
var result = await PopUpDialog().showDeleteDialog();
Or you can also do this if you showDeleteDialog() is async ->
PopUpDialog().showDeleteDialog().then((result){
if (result == 'Success') {
print('success');
setState(() {
data.removeAt(index);
});
} else {
print('fjeodpedp');
}
});
Try this and let me know whether it works or not.
Update
int showDeleteDialog({Function onSuccess, Function onFailure}) {
_bloc = Provider.of<Bloc>(context);
showDialog(
context: context,
builder: (BuildContext buildContext) {
return AlertDialog(
actions: <Widget>[
FlatButton(
color: Colors.orange,
child: Text('YES', style: TextStyle(color: Colors.white)),
onPressed: () async {
Navigator.pop(buildContext);
var result = await _bloc.deleteWorkOrder();
if(onSuccess!=null) onSuccess(result); <------ Here you can pass your result directly to the function.
return result;
},
),
FlatButton(
color: Colors.white,
child: Text('CANCEL'),
onPressed: () {
if(onFailure!=null) onFailure();
Navigator.of(buildContext, rootNavigator: true)
.pop('dialog');
},
)
],
title: Text(Localization.of(buildContext).deleteDialogTitle),
content: Text(Localization.of(buildContext).deleteDialogContent));
});
return 0;
}
Your success function would be like this
onSuccess(result){
if (result == 'Success') {
print('success');
setState(() {
data.removeAt(index);
});
} else {
print('fjeodpedp');
}
}
onFailure(){
//Add your failiure logic here.
}
Upvotes: 3