Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[firebase_admob] Fix firebase_admob issues caused by un-handled exception situations #37

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/firebase_admob/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.9.0+8

* Handle exception in `BannerAd.dispose()` call for `no_ad_for_id` scenario.

## 0.9.0+7

* Update Android gradle plugin, gradle, and Admob versions.
Expand Down
20 changes: 15 additions & 5 deletions packages/firebase_admob/lib/firebase_admob.dart
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ abstract class MobileAd {
/// Disposing a banner ad that's been shown removes it from the screen.
/// Interstitial ads can't be programmatically removed from view.
Future<bool> dispose() {
assert(_allAds[id] != null);
if (_allAds[id] == null) return Future<bool>.value(true);
_allAds[id] = null;
return _invokeBooleanMethod("disposeAd", <String, dynamic>{'id': id});
}
Expand Down Expand Up @@ -520,9 +520,19 @@ class FirebaseAdMob {
}

Future<bool> _invokeBooleanMethod(String method, [dynamic arguments]) async {
final bool result = await FirebaseAdMob.instance._channel.invokeMethod<bool>(
method,
arguments,
);
bool result = false;
try {
result = await FirebaseAdMob.instance._channel.invokeMethod<bool>(
method,
arguments,
);
} on PlatformException catch (e) {
if (e.code == "no_ad_for_id") {
result = false;
return result;
}
rethrow;
}

return result;
}
2 changes: 1 addition & 1 deletion packages/firebase_admob/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: Flutter plugin for Firebase AdMob, supporting
banner, interstitial (full-screen), and rewarded video ads
author: Flutter Team <flutter-dev@googlegroups.com>
homepage: https://github.com/FirebaseExtended/flutterfire/tree/master/packages/firebase_admob
version: 0.9.0+7
version: 0.9.0+8

flutter:
plugin:
Expand Down
30 changes: 30 additions & 0 deletions packages/firebase_admob/test/firebase_admob_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ void main() {
final List<MethodCall> log = <MethodCall>[];
final FirebaseAdMob admob = FirebaseAdMob.private(channel);

int invalidAdId;

setUp(() async {
channel.setMockMethodCallHandler((MethodCall methodCall) async {
log.add(methodCall);
Expand All @@ -29,6 +31,15 @@ void main() {
case 'showAd':
case 'showRewardedVideoAd':
case 'disposeAd':
if (methodCall.arguments != null) {
if (methodCall.arguments.containsKey('id') &&
(invalidAdId != null)) {
if (invalidAdId == methodCall.arguments['id']) {
invalidAdId = null;
throw PlatformException(code: 'no_ad_for_id');
}
}
}
return Future<bool>.value(true);
default:
assert(false);
Expand Down Expand Up @@ -138,5 +149,24 @@ void main() {
isMethodCall('showRewardedVideoAd', arguments: null),
]);
});

test('noAdForId', () async {
log.clear();

final BannerAd banner = BannerAd(
adUnitId: BannerAd.testAdUnitId,
size: AdSize.banner,
);
final int id = banner.id;
invalidAdId = banner.id;

expect(await banner.dispose(), false);

expect(log, <Matcher>[
isMethodCall('disposeAd', arguments: <String, dynamic>{
'id': id,
}),
]);
});
});
}