Skip to content

Instantly share code, notes, and snippets.

@LouisParkin
Created April 22, 2020 07:23
Show Gist options
  • Save LouisParkin/3f16687443c3b1e15507648ccf84161c to your computer and use it in GitHub Desktop.
Save LouisParkin/3f16687443c3b1e15507648ccf84161c to your computer and use it in GitHub Desktop.
Async disposal of a firebase Admob banner in dart.
import 'dart:io';
import 'package:firebase_admob/firebase_admob.dart';
import 'package:flutter/material.dart';
import 'package:sabnzbdplusrewrite/retrofit/models/history.dart';
import 'package:sabnzbdplusrewrite/retrofit/models/queue.dart';
import 'package:sabnzbdplusrewrite/retrofit/models/status.dart' as prefix0;
import './screens/main_activity.dart';
import 'package:shared_preferences/shared_preferences.dart';
SharedPreferences prefs;
Future<prefix0.Status> mStatus;
Future<History> mHistory;
Future<Queue> mQueue;
MobileAdTargetingInfo targetingInfo;
Queue mQueueData;
History mHistoryData;
prefix0.Status mStatusData;
int qStart;
int hStart;
double bottomNavCoordinateX;
double bottomNavCoordinateY;
double upperBotNavCoordinateX;
double upperBotNavCoordinateY;
void main() async {
qStart = 0;
hStart = 0;
prefs = await SharedPreferences.getInstance();
runApp(new MyApp());
}
class MyApp extends StatefulWidget {
// This widget is the root of your application.
@override
State<StatefulWidget> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
void initState() {
try {
mQueue = QueueModel().getQueueData();
mHistory = HistoryModel().getHistoryData();
mStatus = prefix0.StatusModel().getStatusData();
} catch (Exception) {}
String appId = "ca-app-pub-0000000000000000~0000000000";
if (Platform.isAndroid) {
// Android-specific code
appId = "ca-app-pub-0000000000000000~0000000000";
} else if (Platform.isIOS) {
// iOS-specific code
appId = "ca-app-pub-1111111111111111~1111111111";
} else {
appId = "ca-app-pub-2222222222222222~2222222222";
}
FirebaseAdMob.instance
.initialize(appId: appId);
targetingInfo = MobileAdTargetingInfo(
keywords: <String>['flutterio', 'beautiful apps'],
contentUrl: 'https://flutter.io',
childDirected: false,
testDevices: <String>[], // Android emulators are considered test devices
);
super.initState();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'SabNzbd Remote 2.0',
theme: ThemeData(
primarySwatch: Colors.orange,
),
home: MainActivity(title: 'SabNzbd Remote 2.0'),
);
}
}
void disposeBanner(BannerAd banner) {
try {
banner.isLoaded().then((isloaded) {
if (isloaded)
complete(banner);
else
Future.delayed(Duration(seconds: 1), (){
disposeBanner(banner);
});
}, onError: (e){
print(e);
});
} catch (e) {
print("it broke partially with $e");
}
}
void disposeBannerBlock(BannerAd banner) async {
try {
await banner.isLoaded();
completeBlocked(banner);
} catch (e) {
print("it broke partially with $e");
try {
completeBlocked(banner);
} catch (ex) {
print("it broke partially some more with $ex");
}
}
}
void completeBlocked(BannerAd banner) async {
try {
await banner.dispose();
} catch (e) {
print("it broke properly with $e");
}
}
void complete(BannerAd banner) {
try {
banner.dispose();
} catch (e) {
print("it broke properly with $e");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment