From 659acd06908b386b56eb6576c43d23298e26328c Mon Sep 17 00:00:00 2001 From: Harsh Yadav Date: Sat, 20 Jun 2026 03:13:13 +0530 Subject: [PATCH] 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 `///`). [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 --- .../ui/home/view_models/home_viewmodel.dart | 44 +++++++++---------- 1 file changed, 20 insertions(+), 24 deletions(-) diff --git a/compass_app/app/lib/ui/home/view_models/home_viewmodel.dart b/compass_app/app/lib/ui/home/view_models/home_viewmodel.dart index df4a2e479..fba990528 100644 --- a/compass_app/app/lib/ui/home/view_models/home_viewmodel.dart +++ b/compass_app/app/lib/ui/home/view_models/home_viewmodel.dart @@ -65,31 +65,27 @@ class HomeViewModel extends ChangeNotifier { } Future> _deleteBooking(int id) async { - try { - final resultDelete = await _bookingRepository.delete(id); - switch (resultDelete) { - case Ok(): - _log.fine('Deleted booking $id'); - case Error(): - _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>(): - _bookings = resultLoadBookings.value; - _log.fine('Loaded bookings'); - case Error>(): - _log.warning('Failed to load bookings', resultLoadBookings.error); - return resultLoadBookings; - } + final resultDelete = await _bookingRepository.delete(id); + switch (resultDelete) { + case Ok(): + _log.fine('Deleted booking $id'); + case Error(): + _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>(): + _bookings = resultLoadBookings.value; + _log.fine('Loaded bookings'); + notifyListeners(); // notify only when data changes + case Error>(): + _log.warning('Failed to load bookings', resultLoadBookings.error); + return resultLoadBookings; } + + return resultLoadBookings; } }