Skip to content

Commit

Permalink
Null safety refactor (#99)
Browse files Browse the repository at this point in the history
* refactor: Migrate to null safety

Co-authored-by: untp <untitledpermission@gmail.com>
  • Loading branch information
jjliu15 and untp authored Mar 29, 2021
1 parent b32a33b commit 052dcd4
Show file tree
Hide file tree
Showing 15 changed files with 333 additions and 276 deletions.
7 changes: 6 additions & 1 deletion analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.

include: package:pedantic/analysis_options.1.8.0.yaml
include: package:pedantic/analysis_options.1.11.0.yaml

analyzer:
errors:
omit_local_variable_types: ignore

linter:
rules:
- public_member_api_docs
4 changes: 4 additions & 0 deletions packages/google_mobile_ads/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.12.0

* Migrated to null safety. Minimum Dart SDK version is bumped to 2.12.0.

## 0.11.0+4

* Fixes a [bug](https://github.com/googleads/googleads-mobile-flutter/issues/47) where state is not properly cleaned up on hot restart.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
/** Constants used in the plugin. */
public class Constants {
/** Version request agent. Should be bumped alongside plugin versions. */
public static final String REQUEST_AGENT_PREFIX_VERSIONED = "Flutter-GMA-0.11.0+4";
public static final String REQUEST_AGENT_PREFIX_VERSIONED = "Flutter-GMA-0.12.0";
}
15 changes: 15 additions & 0 deletions packages/google_mobile_ads/example/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

include: ../../../analysis_options.yaml
62 changes: 30 additions & 32 deletions packages/google_mobile_ads/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ class MyApp extends StatefulWidget {

class _MyAppState extends State<MyApp> {
static final AdRequest request = AdRequest(
testDevices: testDevice != null ? <String>[testDevice] : null,
testDevices: <String>[testDevice],
keywords: <String>['foo', 'bar'],
contentUrl: 'http://foo.com/bar.html',
nonPersonalizedAds: true,
);

InterstitialAd _interstitialAd;
InterstitialAd? _interstitialAd;
bool _interstitialReady = false;

RewardedAd _rewardedAd;
RewardedAd? _rewardedAd;
bool _rewardedReady = false;

@override
Expand All @@ -54,7 +54,7 @@ class _MyAppState extends State<MyApp> {
.updateRequestConfiguration(RequestConfiguration(
tagForChildDirectedTreatment:
TagForChildDirectedTreatment.unspecified))
.then((value) {
.then((void value) {
createInterstitialAd();
createRewardedAd();
});
Expand Down Expand Up @@ -130,7 +130,7 @@ class _MyAppState extends State<MyApp> {
Widget build(BuildContext context) {
return MaterialApp(
home: Builder(
builder: (context) => Scaffold(
builder: (BuildContext context) => Scaffold(
appBar: AppBar(
title: const Text('AdMob Plugin example app'),
actions: <Widget>[
Expand All @@ -139,39 +139,40 @@ class _MyAppState extends State<MyApp> {
switch (result) {
case 'InterstitialAd':
if (!_interstitialReady) return;
_interstitialAd.show();
_interstitialAd!.show();
_interstitialReady = false;
_interstitialAd = null;
break;
case 'RewardedAd':
if (!_rewardedReady) return;
_rewardedAd.show();
_rewardedAd!.show();
_rewardedReady = false;
_rewardedAd = null;
break;
case 'ReusableInlineExample':
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ReusableInlineExample()),
MaterialPageRoute<void>(
builder: (BuildContext context) =>
ReusableInlineExample()),
);
break;
default:
throw AssertionError('unexpected button: ${result}');
throw AssertionError('unexpected button: $result');
}
},
itemBuilder: (BuildContext context) => <PopupMenuEntry<String>>[
PopupMenuItem<String>(
child: Text('$InterstitialAd'),
value: '$InterstitialAd',
child: Text('$InterstitialAd'),
),
PopupMenuItem<String>(
child: Text('$RewardedAd'),
value: '$RewardedAd',
child: Text('$RewardedAd'),
),
PopupMenuItem<String>(
child: Text('Reusable Inline Ads Object Example'),
value: 'ReusableInlineExample',
child: Text('Reusable Inline Ads Object Example'),
),
],
),
Expand Down Expand Up @@ -224,7 +225,7 @@ class BannerAdWidget extends StatefulWidget {
}

class BannerAdState extends State<BannerAdWidget> {
BannerAd _bannerAd;
late BannerAd _bannerAd;
final Completer<BannerAd> bannerCompleter = Completer<BannerAd>();

@override
Expand All @@ -242,21 +243,20 @@ class BannerAdState extends State<BannerAdWidget> {
onAdFailedToLoad: (Ad ad, LoadAdError error) {
ad.dispose();
print('$BannerAd failedToLoad: $error');
bannerCompleter.completeError(null);
bannerCompleter.completeError(error);
},
onAdOpened: (Ad ad) => print('$BannerAd onAdOpened.'),
onAdClosed: (Ad ad) => print('$BannerAd onAdClosed.'),
onApplicationExit: (Ad ad) => print('$BannerAd onApplicationExit.'),
),
);
Future<void>.delayed(Duration(seconds: 1), () => _bannerAd?.load());
Future<void>.delayed(Duration(seconds: 1), () => _bannerAd.load());
}

@override
void dispose() {
super.dispose();
_bannerAd?.dispose();
_bannerAd = null;
_bannerAd.dispose();
}

@override
Expand All @@ -283,8 +283,8 @@ class BannerAdState extends State<BannerAdWidget> {
return Container(
width: _bannerAd.size.width.toDouble(),
height: _bannerAd.size.height.toDouble(),
child: child,
color: Colors.blueGrey,
child: child,
);
},
);
Expand All @@ -301,7 +301,7 @@ class PublisherBannerAdWidget extends StatefulWidget {
}

class PublisherBannerAdState extends State<PublisherBannerAdWidget> {
PublisherBannerAd _bannerAd;
late PublisherBannerAd _bannerAd;
final Completer<PublisherBannerAd> bannerCompleter =
Completer<PublisherBannerAd>();

Expand All @@ -311,7 +311,7 @@ class PublisherBannerAdState extends State<PublisherBannerAdWidget> {
_bannerAd = PublisherBannerAd(
adUnitId: '/6499/example/banner',
request: PublisherAdRequest(nonPersonalizedAds: true),
sizes: [widget.size],
sizes: <AdSize>[widget.size],
listener: AdListener(
onAdLoaded: (Ad ad) {
print('$PublisherBannerAd loaded.');
Expand All @@ -320,22 +320,21 @@ class PublisherBannerAdState extends State<PublisherBannerAdWidget> {
onAdFailedToLoad: (Ad ad, LoadAdError error) {
ad.dispose();
print('$PublisherBannerAd failedToLoad: $error');
bannerCompleter.completeError(null);
bannerCompleter.completeError(error);
},
onAdOpened: (Ad ad) => print('$PublisherBannerAd onAdOpened.'),
onAdClosed: (Ad ad) => print('$PublisherBannerAd onAdClosed.'),
onApplicationExit: (Ad ad) =>
print('$PublisherBannerAd onApplicationExit.'),
),
);
Future<void>.delayed(Duration(seconds: 1), () => _bannerAd?.load());
Future<void>.delayed(Duration(seconds: 1), () => _bannerAd.load());
}

@override
void dispose() {
super.dispose();
_bannerAd?.dispose();
_bannerAd = null;
_bannerAd.dispose();
}

@override
Expand Down Expand Up @@ -363,8 +362,8 @@ class PublisherBannerAdState extends State<PublisherBannerAdWidget> {
return Container(
width: _bannerAd.sizes[0].width.toDouble(),
height: _bannerAd.sizes[0].height.toDouble(),
child: child,
color: Colors.blueGrey,
child: child,
);
},
);
Expand All @@ -377,7 +376,7 @@ class NativeAdWidget extends StatefulWidget {
}

class NativeAdState extends State<NativeAdWidget> {
NativeAd _nativeAd;
late NativeAd _nativeAd;
final Completer<NativeAd> nativeAdCompleter = Completer<NativeAd>();

@override
Expand All @@ -395,21 +394,20 @@ class NativeAdState extends State<NativeAdWidget> {
onAdFailedToLoad: (Ad ad, LoadAdError error) {
ad.dispose();
print('$NativeAd failedToLoad: $error');
nativeAdCompleter.completeError(null);
nativeAdCompleter.completeError(error);
},
onAdOpened: (Ad ad) => print('$NativeAd onAdOpened.'),
onAdClosed: (Ad ad) => print('$NativeAd onAdClosed.'),
onApplicationExit: (Ad ad) => print('$NativeAd onApplicationExit.'),
),
);
Future<void>.delayed(Duration(seconds: 1), () => _nativeAd?.load());
Future<void>.delayed(Duration(seconds: 1), () => _nativeAd.load());
}

@override
void dispose() {
super.dispose();
_nativeAd?.dispose();
_nativeAd = null;
_nativeAd.dispose();
}

@override
Expand All @@ -436,8 +434,8 @@ class NativeAdState extends State<NativeAdWidget> {
return Container(
width: 250,
height: 350,
child: child,
color: Colors.blueGrey,
child: child,
);
},
);
Expand Down
41 changes: 23 additions & 18 deletions packages/google_mobile_ads/example/lib/reusable_inline_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ class ReusableInlineExample extends StatefulWidget {
}

class _ReusableInlineExampleState extends State<ReusableInlineExample> {
BannerAd _bannerAd;
BannerAd? _bannerAd;
bool _bannerAdIsLoaded = false;

PublisherBannerAd _publisherBannerAd;
PublisherBannerAd? _publisherBannerAd;
bool _publisherBannerAdIsLoaded = false;

NativeAd _nativeAd;
NativeAd? _nativeAd;
bool _nativeAdIsLoaded = false;

@override
Expand All @@ -39,23 +39,28 @@ class _ReusableInlineExampleState extends State<ReusableInlineExample> {
);
},
itemBuilder: (BuildContext context, int index) {
if (index == 5 && _bannerAdIsLoaded && _bannerAd != null) {
final BannerAd? bannerAd = _bannerAd;
if (index == 5 && _bannerAdIsLoaded && bannerAd != null) {
return Container(
height: _bannerAd.size.height.toDouble(),
width: _bannerAd.size.width.toDouble(),
child: AdWidget(ad: _bannerAd));
height: bannerAd.size.height.toDouble(),
width: bannerAd.size.width.toDouble(),
child: AdWidget(ad: bannerAd));
}

final PublisherBannerAd? publisherBannerAd = _publisherBannerAd;
if (index == 10 &&
_publisherBannerAdIsLoaded &&
_publisherBannerAd != null) {
publisherBannerAd != null) {
return Container(
height: _publisherBannerAd.sizes[0].height.toDouble(),
width: _publisherBannerAd.sizes[0].width.toDouble(),
child: AdWidget(ad: _publisherBannerAd));
height: publisherBannerAd.sizes[0].height.toDouble(),
width: publisherBannerAd.sizes[0].width.toDouble(),
child: AdWidget(ad: _publisherBannerAd!));
}
if (index == 15 && _nativeAdIsLoaded && _nativeAd != null) {

final NativeAd? nativeAd = _nativeAd;
if (index == 15 && _nativeAdIsLoaded && nativeAd != null) {
return Container(
width: 250, height: 350, child: AdWidget(ad: _nativeAd));
width: 250, height: 350, child: AdWidget(ad: nativeAd));
}

return Text(
Expand All @@ -81,7 +86,7 @@ class _ReusableInlineExampleState extends State<ReusableInlineExample> {
onAdLoaded: (Ad ad) {
print('$BannerAd loaded.');
setState(() {
this._bannerAdIsLoaded = true;
_bannerAdIsLoaded = true;
});
},
onAdFailedToLoad: (Ad ad, LoadAdError error) {
Expand All @@ -105,11 +110,11 @@ class _ReusableInlineExampleState extends State<ReusableInlineExample> {
onAdLoaded: (Ad ad) {
print('$NativeAd loaded.');
setState(() {
this._nativeAdIsLoaded = true;
_nativeAdIsLoaded = true;
});
},
onAdFailedToLoad: (Ad ad, LoadAdError error) {
print('$NativeAd failedToLoad: ${error}');
print('$NativeAd failedToLoad: $error');
ad.dispose();
},
onAdOpened: (Ad ad) => print('$NativeAd onAdOpened.'),
Expand All @@ -121,12 +126,12 @@ class _ReusableInlineExampleState extends State<ReusableInlineExample> {
_publisherBannerAd = PublisherBannerAd(
adUnitId: '/6499/example/banner',
request: PublisherAdRequest(nonPersonalizedAds: true),
sizes: [AdSize.largeBanner],
sizes: <AdSize>[AdSize.largeBanner],
listener: AdListener(
onAdLoaded: (Ad ad) {
print('$PublisherBannerAd loaded.');
setState(() {
this._publisherBannerAdIsLoaded = true;
_publisherBannerAdIsLoaded = true;
});
},
onAdFailedToLoad: (Ad ad, LoadAdError error) {
Expand Down
6 changes: 3 additions & 3 deletions packages/google_mobile_ads/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ dependencies:
path: ../

dev_dependencies:
pedantic: ^1.8.0
e2e: ^0.2.1
pedantic: ^1.11.0
e2e: ^0.7.0
flutter_driver:
sdk: flutter

flutter:
uses-material-design: true

environment:
sdk: ">=2.0.0 <3.0.0"
sdk: ">=2.12.0 <3.0.0"
2 changes: 1 addition & 1 deletion packages/google_mobile_ads/ios/Classes/FLTConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
// limitations under the License.

/** Versioned request agent string. */
#define FLT_REQUEST_AGENT_VERSIONED @"Flutter-GMA-0.11.0+4"
#define FLT_REQUEST_AGENT_VERSIONED @"Flutter-GMA-0.12.0"
Loading

0 comments on commit 052dcd4

Please sign in to comment.