Skip to content

Commit 7a4366c

Browse files
committed
fix(headlines-feed): dispatch initial fetch event in _HeadlinesFeedPageState
- Move initial fetch event dispatch from router's BlocProvider to _HeadlinesFeedPageState::initState - Use WidgetsBinding.instance.addPostFrameCallback to ensure BuildContext is fully initialized - Prevent "Tried to listen to an InheritedWidget in a life-cycle that will never be called again" error
1 parent 2325e42 commit 7a4366c

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

lib/headlines-feed/view/headlines_feed_page.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,23 @@ class _HeadlinesFeedPageState extends State<HeadlinesFeedPage> {
4343
super.initState();
4444
// Add listener to trigger pagination when scrolling near the bottom.
4545
_scrollController.addListener(_onScroll);
46+
47+
// Dispatch the initial fetch event for the headlines feed.
48+
// This is intentionally placed in `initState` and wrapped in `addPostFrameCallback`
49+
// to ensure the `BuildContext` is fully initialized and stable.
50+
// This prevents the "Tried to listen to an InheritedWidget in a life-cycle
51+
// that will never be called again" error, which occurred when the event
52+
// was dispatched from the router's `BlocProvider` `create` method,
53+
// as that context could be disposed before asynchronous operations completed.
54+
WidgetsBinding.instance.addPostFrameCallback((_) {
55+
if (mounted) {
56+
context.read<HeadlinesFeedBloc>().add(
57+
HeadlinesFeedFetchRequested(
58+
adThemeStyle: AdThemeStyle.fromTheme(Theme.of(context)),
59+
),
60+
);
61+
}
62+
});
4663
}
4764

4865
@override

0 commit comments

Comments
 (0)