This is my code where I am showing list of pending invoices.But when i accept the it should reload the pending invoice widget after clicking on snack bar action button.When user clicks on accept button its showing snack bar and when pressed accept action button snack bar it navigate back to same widget where I needs to update that widget along with new list of pending invoices without the invoice which we have accepted
Share
You can use the
setState
method to update the state of your widget and trigger a rebuild of the widget with the updated list of pending invoices. In the callback function for the snack bar’s “accept” action button, you can callsetState
to update the list of pending invoices and remove the invoice that was accepted. You can pass a function tosetState
that takes the previous state as an argument and returns the updated state, like so:setState((prevState) {
return {
...prevState,
pendingInvoices: prevState.pendingInvoices.where((invoice) => invoice.id != acceptedInvoiceId).toList()
};
});
This code will remove the invoice from the list using
where
method.You can also use the
update
method of theFuture
class in the callback function for the snack bar’s “accept” action button. This will update the list of pending invoices and remove the invoice that was accepted.Future.delayed(Duration(seconds: 2),(){
setState(() {
pendingInvoices = pendingInvoices.where((invoice) => invoice.id != acceptedInvoiceId).toList();
});
});
This will wait for 2 seconds before updating the state and removing the invoice from the list. Please make sure you are passing correct invoice id.