Skip to content

Commit 4fc3846

Browse files
committed
fix(archived_local_ads_page): Prevent "deactivated widget's ancestor" error
- Replace context.read in SnackBar action with direct service call - Remove unnecessary state check for restoredLocalAd - Update BlocListener to avoid redundant UI updates
1 parent 7533803 commit 4fc3846

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

lib/local_ads_management/view/archived_local_ads_page.dart

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ class _ArchivedLocalAdsViewState extends State<_ArchivedLocalAdsView>
7979
@override
8080
Widget build(BuildContext context) {
8181
final l10n = AppLocalizationsX(context).l10n;
82+
// Read the PendingDeletionsService instance directly.
83+
// This is safe because PendingDeletionsService is a singleton and does not
84+
// depend on the widget's BuildContext for its operations, preventing
85+
// "deactivated widget's ancestor" errors when the SnackBar is dismissed
86+
// after the widget has been unmounted.
87+
final pendingDeletionsService = context.read<PendingDeletionsService>();
88+
8289
return Scaffold(
8390
appBar: AppBar(
8491
title: Text(l10n.archivedLocalAdsTitle),
@@ -92,9 +99,7 @@ class _ArchivedLocalAdsViewState extends State<_ArchivedLocalAdsView>
9299
body: BlocListener<ArchiveLocalAdsBloc, ArchiveLocalAdsState>(
93100
listenWhen: (previous, current) =>
94101
previous.lastPendingDeletionId != current.lastPendingDeletionId ||
95-
previous.snackbarLocalAdTitle != current.snackbarLocalAdTitle ||
96-
(previous.restoredLocalAd == null &&
97-
current.restoredLocalAd != null),
102+
previous.snackbarLocalAdTitle != current.snackbarLocalAdTitle,
98103
listener: (context, state) {
99104
if (state.snackbarLocalAdTitle != null) {
100105
final adId = state.lastPendingDeletionId!;
@@ -109,23 +114,15 @@ class _ArchivedLocalAdsViewState extends State<_ArchivedLocalAdsView>
109114
action: SnackBarAction(
110115
label: l10n.undo,
111116
onPressed: () {
112-
context.read<ArchiveLocalAdsBloc>().add(
113-
const UndoDeleteLocalAdRequested(),
114-
);
117+
// Directly call undoDeletion on the service.
118+
// This avoids using context.read on a potentially deactivated
119+
// widget, which caused the "deactivated widget's ancestor" error.
120+
pendingDeletionsService.undoDeletion(adId);
115121
},
116122
),
117123
),
118124
);
119125
}
120-
// Trigger refresh of active ads in LocalAdsManagementBloc if an ad was restored
121-
if (state.restoredLocalAd != null) {
122-
context.read<LocalAdsManagementBloc>().add(
123-
LoadLocalAdsRequested(
124-
adType: state.restoredLocalAd!.toAdType(),
125-
forceRefresh: true,
126-
),
127-
);
128-
}
129126
},
130127
child: TabBarView(
131128
controller: _tabController,

0 commit comments

Comments
 (0)