I’m trying to display an interstitial ad from Google AdMob in my Flutter app, but I’m encountering an issue when attempting to show the ad from within a showModalBottomSheet
context.
Here’s the relevant code:
void show() {
showModalBottomSheet(
context: context,
constraints: const BoxConstraints(
maxWidth: double.infinity,
),
builder: (context) {
return AudioConverter();
}
);
}
class _AudioConverterState extends State<AudioConverter> {
@override
Widget build(BuildContext context) {
return Container(
// ...
child: ElevatedButton(
onPressed: displayAd,
child: Text('Show Ad'),
),
);
}
void displayAd() async {
if (interstitialAd != null) {
interstitialAd!.fullScreenContentCallback =
FullScreenContentCallback(onAdDismissedFullScreenContent: (ad) {
// ...
}, onAdFailedToShowFullScreenContent: (ad, adError) {
print(adError); // the provided view controller is not being presented
});
interstitialAd!.show();
}
}
}
When I tap the “Show Ad” button, which calls the displayAd function, the onAdFailedToShowFullScreenContent callback is invoked, and I get the following error:
error(code: 17, domain: com.google.admob, message: the provided view controller is not being presented.)
I suspect that this issue is occurring because I’m trying to display the interstitial ad from within the AudioConverter widget, which is inside the showModalBottomSheet context.
Can someone please explain why this error is happening and provide a solution to display the interstitial ad correctly in this scenario?