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>
pull/2866/head
Harsh Yadav 2 days ago committed by GitHub
parent effe13307c
commit 659acd0690
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -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;
}
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;
}
return resultLoadBookings;
} finally {
notifyListeners();
// 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;
}
}

Loading…
Cancel
Save