Skip to content

Commit 8e4dc1d

Browse files
committed
refactor(ads): conditionally import and use AdMobAdProvider
- Add kIsWeb check to use DemoAdProvider on web platforms - Implement conditional import for AdMobAdProvider - Update ad provider logic to use DemoAdProvider on web or in demo environment - These changes prevent potential issues with AdMob SDK on web platforms - Improve code maintainability and platform compatibility
1 parent b9b0593 commit 8e4dc1d

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

lib/bootstrap.dart

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,17 @@ 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' show kIsWeb; // Import kIsWeb
1011
import 'package:flutter/material.dart';
1112
import 'package:flutter_bloc/flutter_bloc.dart';
1213
import 'package:flutter_news_app_mobile_client_full_source_code/ads/ad_provider.dart';
1314
import 'package:flutter_news_app_mobile_client_full_source_code/ads/ad_service.dart';
14-
import 'package:flutter_news_app_mobile_client_full_source_code/ads/admob_ad_provider.dart';
15-
import 'package:flutter_news_app_mobile_client_full_source_code/ads/demo_ad_provider.dart'; // Added DemoAdProvider
15+
// Conditional import for AdMobAdProvider
16+
// This ensures the AdMob package is only imported when not on the web,
17+
// preventing potential issues or unnecessary logs on web platforms.
18+
import 'package:flutter_news_app_mobile_client_full_source_code/ads/admob_ad_provider.dart'
19+
if (dart.library.io) 'package:flutter_news_app_mobile_client_full_source_code/ads/admob_ad_provider.dart';
20+
import 'package:flutter_news_app_mobile_client_full_source_code/ads/demo_ad_provider.dart';
1621
import 'package:flutter_news_app_mobile_client_full_source_code/ads/local_ad_provider.dart';
1722
import 'package:flutter_news_app_mobile_client_full_source_code/app/app.dart';
1823
import 'package:flutter_news_app_mobile_client_full_source_code/app/config/config.dart'
@@ -90,17 +95,19 @@ Future<Widget> bootstrap(
9095
// Conditionally instantiate ad providers based on the application environment.
9196
// This ensures that only the relevant ad providers are available for the
9297
// current environment, preventing unintended usage.
93-
if (appConfig.environment == app_config.AppEnvironment.demo) {
98+
if (appConfig.environment == app_config.AppEnvironment.demo || kIsWeb) {
9499
final demoAdProvider = DemoAdProvider(logger: logger);
95100
adProviders = {
96-
// In the demo environment, all ad platform types map to the DemoAdProvider.
97-
// This simulates ad behavior without actual network calls.
101+
// In the demo environment or on the web, all ad platform types map to
102+
// the DemoAdProvider. This simulates ad behavior without actual network
103+
// calls and avoids issues with platform-specific ad SDKs on unsupported
104+
// platforms (e.g., AdMob on web).
98105
AdPlatformType.admob: demoAdProvider,
99106
AdPlatformType.local: demoAdProvider,
100107
AdPlatformType.demo: demoAdProvider,
101108
};
102109
} else {
103-
// For development and production environments, use real ad providers.
110+
// For development and production environments (non-web), use real ad providers.
104111
adProviders = {
105112
// AdMob provider for Google Mobile Ads.
106113
AdPlatformType.admob: AdMobAdProvider(logger: logger),
@@ -117,7 +124,7 @@ Future<Widget> bootstrap(
117124
),
118125
logger: logger,
119126
),
120-
// The demo ad platform is not available in non-demo environments.
127+
// The demo ad platform is not available in non-demo/non-web environments.
121128
// If AdService attempts to access it, it will receive null, which is
122129
// handled by AdService's internal logic (logging a warning).
123130
};

0 commit comments

Comments
 (0)