Skip to content

Commit 09f7e82

Browse files
authored
Merge pull request #86 from flutter-news-app-full-source-code/fix-Decouple-Initial-Data-Fetch-from-Router-Configuration
Fix decouple initial data fetch from router configuration
2 parents 881b101 + 9116e01 commit 09f7e82

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

lib/bootstrap.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@ import 'package:data_api/data_api.dart';
77
import 'package:data_client/data_client.dart';
88
import 'package:data_inmemory/data_inmemory.dart';
99
import 'package:data_repository/data_repository.dart';
10+
import 'package:flutter/foundation.dart';
1011
import 'package:flutter/material.dart';
1112
import 'package:flutter_bloc/flutter_bloc.dart';
12-
import 'package:flutter_news_app_mobile_client_full_source_code/ads/ad_provider.dart';
1313
import 'package:flutter_news_app_mobile_client_full_source_code/ads/ad_service.dart';
14-
import 'package:flutter/foundation.dart';
1514
import 'package:flutter_news_app_mobile_client_full_source_code/ads/admob_ad_provider.dart';
1615
import 'package:flutter_news_app_mobile_client_full_source_code/ads/no_op_ad_provider.dart';
1716
import 'package:flutter_news_app_mobile_client_full_source_code/app/app.dart';
@@ -47,7 +46,7 @@ Future<Widget> bootstrap(
4746
// Initialize AdProvider based on platform.
4847
// On web, use a No-Op provider to prevent MissingPluginException,
4948
// as Google Mobile Ads SDK does not support native ads on web.
50-
final AdProvider adProvider = kIsWeb
49+
final adProvider = kIsWeb
5150
? NoOpAdProvider(logger: logger)
5251
: AdMobAdProvider(logger: logger);
5352

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

lib/router/router.dart

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import 'package:flutter_news_app_mobile_client_full_source_code/account/view/man
1212
import 'package:flutter_news_app_mobile_client_full_source_code/account/view/manage_followed_items/topics/followed_topics_list_page.dart';
1313
import 'package:flutter_news_app_mobile_client_full_source_code/account/view/saved_headlines_page.dart';
1414
import 'package:flutter_news_app_mobile_client_full_source_code/ads/ad_service.dart';
15-
import 'package:flutter_news_app_mobile_client_full_source_code/ads/models/ad_theme_style.dart';
1615
import 'package:flutter_news_app_mobile_client_full_source_code/app/bloc/app_bloc.dart';
1716
import 'package:flutter_news_app_mobile_client_full_source_code/app/config/config.dart'
1817
as local_config;
@@ -372,10 +371,6 @@ GoRouter createRouter({
372371
.read<DataRepository<UserContentPreferences>>(),
373372
feedDecoratorService: feedDecoratorService,
374373
appBloc: context.read<AppBloc>(),
375-
)..add(
376-
HeadlinesFeedFetchRequested(
377-
adThemeStyle: AdThemeStyle.fromTheme(Theme.of(context)),
378-
),
379374
);
380375
},
381376
),

0 commit comments

Comments
 (0)