mirror of
https://github.com/flutter/samples.git
synced 2026-06-25 07:38:26 +00:00
refactor: remove redundant notifyListeners in HomeViewModel deleteBooking (#2839)
Removes redundant notifyListeners() call from _deleteBooking(). The method is executed through Command, which already manages its own notifyListeners lifecycle. This change ensures that HomeViewModel only notifies listeners when its internal state (_bookings) changes, avoiding unnecessary updates. Fixes https://github.com/flutter/samples/issues/2746 ## Pre-launch Checklist - [x] I read the [Flutter Style Guide] recently, and have followed its advice. - [x] I signed the [CLA]. - [ ] I have added sample code updates to the [changelog]. - [ ] I updated/added relevant documentation (doc comments with `///`). <!-- Links --> [Flutter Style Guide]: https://github.com/flutter/flutter/blob/master/docs/contributing/Style-guide-for-Flutter-repo.md [CLA]: https://cla.developers.google.com/ [changelog]: ./CHANGELOG.md Co-authored-by: Eric Windmill <eric@ericwindmill.com>
This commit is contained in:
@@ -65,31 +65,27 @@ class HomeViewModel extends ChangeNotifier {
|
||||
}
|
||||
|
||||
Future<Result<void>> _deleteBooking(int id) async {
|
||||
try {
|
||||
final resultDelete = await _bookingRepository.delete(id);
|
||||
switch (resultDelete) {
|
||||
case Ok<void>():
|
||||
_log.fine('Deleted booking $id');
|
||||
case Error<void>():
|
||||
_log.warning('Failed to delete booking $id', resultDelete.error);
|
||||
return resultDelete;
|
||||
}
|
||||
|
||||
// After deleting the booking, we need to reload the bookings list.
|
||||
// BookingRepository is the source of truth for bookings.
|
||||
final resultLoadBookings = await _bookingRepository.getBookingsList();
|
||||
switch (resultLoadBookings) {
|
||||
case Ok<List<BookingSummary>>():
|
||||
_bookings = resultLoadBookings.value;
|
||||
_log.fine('Loaded bookings');
|
||||
case Error<List<BookingSummary>>():
|
||||
_log.warning('Failed to load bookings', resultLoadBookings.error);
|
||||
return resultLoadBookings;
|
||||
}
|
||||
|
||||
return resultLoadBookings;
|
||||
} finally {
|
||||
notifyListeners();
|
||||
final resultDelete = await _bookingRepository.delete(id);
|
||||
switch (resultDelete) {
|
||||
case Ok<void>():
|
||||
_log.fine('Deleted booking $id');
|
||||
case Error<void>():
|
||||
_log.warning('Failed to delete booking $id', resultDelete.error);
|
||||
return resultDelete;
|
||||
}
|
||||
|
||||
// After deleting the booking, reload the bookings list.
|
||||
final resultLoadBookings = await _bookingRepository.getBookingsList();
|
||||
switch (resultLoadBookings) {
|
||||
case Ok<List<BookingSummary>>():
|
||||
_bookings = resultLoadBookings.value;
|
||||
_log.fine('Loaded bookings');
|
||||
notifyListeners(); // notify only when data changes
|
||||
case Error<List<BookingSummary>>():
|
||||
_log.warning('Failed to load bookings', resultLoadBookings.error);
|
||||
return resultLoadBookings;
|
||||
}
|
||||
|
||||
return resultLoadBookings;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user