initAdNetworks method
Future<void>
initAdNetworks(
{ - required BuildContext context,
- required dynamic onInitComplete(),
})
Implementation
Future<void> initAdNetworks({
required BuildContext context,
required Function() onInitComplete,
}) async {
MainJson mainJson = context.read<MainJson>();
try {
// ATT must run before UMP consent and MobileAds init on iOS.
if (Platform.isIOS) {
final status =
await AppTrackingTransparency.trackingAuthorizationStatus;
if (status == TrackingStatus.notDetermined) {
await Future.delayed(const Duration(milliseconds: 500));
await AppTrackingTransparency.requestTrackingAuthorization();
}
}
// Null-safe: a config missing `adNetwork` or `ad_config` used to throw
// here and the app never got past startup.
final Map? networks =
mainJson.data?['version_config']?[mainJson.version]?['adNetwork'];
final bool googleEnabled = networks?['google'] ?? false;
final bool appLovinEnabled = networks?['appLovin'] ?? false;
if (!googleEnabled && !appLovinEnabled) {
mainJson.isAdsOn = false;
}
if (googleEnabled) {
// Non-blocking - UMP consent + MobileAds.initialize run in background.
GoogleInit().onInit();
} else {
// Google not enabled - resolve the completer so any await
// GoogleInit.ready calls in GoogleBanner/GoogleNative don't hang.
GoogleInit.skip();
}
if (appLovinEnabled) {
// Non-blocking - AppLovinMAX.initialize() runs in background.
final Object? configuredKey =
mainJson.data?['ad_config']?['applovin_sdk_key'];
AppLovinInit.init(
(configuredKey is String && configuredKey.isNotEmpty)
? configuredKey
: "xiAs_Fs3BiExPelVuawzyDTU2Sy4GL2d6KB1c7C1loiv64T5oquTwRRIJbHC3qO0qRI_65NChIkGy3U2i6rWXn",
);
} else {
// AppLovin not enabled - resolve the completer so any await
// AppLovinInit.ready calls in the AppLovin loaders don't hang.
AppLovinInit.skip();
}
} catch (e) {
debugPrint('Adhub: ad network init failed: $e');
mainJson.isAdsOn = false;
GoogleInit.skip();
AppLovinInit.skip();
}
// Always continue into the app, even when ads could not be set up.
Future.delayed(const Duration(milliseconds: 100), () {
onInitComplete();
});
}