I was using google_mobile_ads
plugin to display ad as suggested in this codebook.
I tried this on my existing app with camera
and tflite
plugins to display a banner ad. I am able to display the ad successfully, but the camera completely hangs(in emulator, oneplus5, samsung m31). In the emulator, I am unable to move the camera around in virtual environment and phone cameras drop to probably 1fps and feel stuck. The problem remains even when a live ad isn't loaded and nothing is displayed. The same code built on iOS
doesn't seem to have any issue.
As soon as I remove the Adwidget
, and add a simple Text
, the problem goes away. Following is my widget build code:
Widget build(BuildContext context) {
Size screen = MediaQuery.of(context).size;
return Container(
child: Stack(
children: [
Camera(
widget.cameras,
_loadedModel,
setRecognitions,
),
Container(
width: screen.width,
height: _ad.size.height.toDouble(),
child:Container(
// child: Text("XZCXZCZXCXZC"), // Uncommenting this and commenting Adwidget code makes the hang/lag issue go away
child: AdWidget(ad: _ad),
width: _ad.size.width.toDouble(),
height: _ad.size.height.toDouble(),
alignment: Alignment.topCenter,
),)
])
);
}
And following is my bannerad initialization code written in initState
(similar to the cookbook)
_ad = BannerAd(
adUnitId: AdManager.bannerAdUnitId,
size: AdSize.banner,
request: adRequest,
listener: AdListener(
onAdLoaded: (_) {
setState(() {
_isAdLoaded = true;
});
},
onAdFailedToLoad: (ad, error) {
// Releases an ad resource when it fails to load
ad.dispose();
print('Ad load failed (code=${error.code} message=${error.message})');
},
),
);
_ad.load();