'google_mobile_ads occurs an error when banner widget displayed | flutter
I'm trying to display banner ad in my app on Android.
But an error occurs when AdWidget() called.
throw FlutterError.fromParts(<DiagnosticsNode>[
...information,
DiagnosticsProperty<BoxConstraints>('The constraints that applied to the $runtimeType were', constraints, style: DiagnosticsTreeStyle.errorProperty),
DiagnosticsProperty<Size>('The exact size it was given was', _size, style: DiagnosticsTreeStyle.errorProperty),
ErrorHint('See https://flutter.dev/docs/development/ui/layout/box-constraints for more information.'),
]);
But I couldn't find the error in log. VScode just showed me this box.dart marked line.
What I Did
Edit AndroidManifest.xml
Edited AndroidManifest.xml in android/app/src/main.
<application>
.....
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="***********************************"/>
....
</application>
Initialize Admob
void main() async{
WidgetsFlutterBinding.ensureInitialized();
await MobileAds.instance.initialize();
}
Create instance in build()
// In class extends StatefulWidget's State
@override
Widget build(BuildContext context) {
BannerAd bannerAd = BannerAd(
adUnitId: getBannerAdUnitId(),
size: AdSize.banner,
request: const AdRequest(),
listener: const BannerAdListener(),
);
bannerAd.load();
.....
Call AdWidget(bannerAd)
return Center(
child: Column(
children: [
AdWidget(bannerAd),
..........
]);
When I remove AdWidget(bannerAd) from the code, no error has shown.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

