Flutter Load Ad Error in using google_mobile_ads package

Viewed 241

I am getting an error like Load Ad Error when using the google_mobile_ads package in Flutter . The complete error I got is: LoadAdError(code: 1, domain: com.google.android.gms.ads, message: Error building request URL., responseInfo: ResponseInfo(responseId: null, mediationAdapterClassName: , adapterResponses: [])). The Code I Wrote:

 InterstitialAd? _interstitialName;
int _numInterstitialLoadAttempts = 0;
final AdRequest request = AdRequest(
  keywords: <String>[
"Music","Education","Music Education"
  ],
  contentUrl: '',
  nonPersonalizedAds: true,
);
const int maxFailedLoadAttempts = 3;

void _showInterstitialAd() {
  if (_interstitialAd == null) {
    print('Warning: attempt to show interstitial before loaded.');
    return;
  }
  _interstitialAd!.fullScreenContentCallback = FullScreenContentCallback(
    onAdShowedFullScreenContent: (InterstitialAd name) {
      print('ad onAdShowedFullScreenContent.');
    },
    onAdDismissedFullScreenContent: (InterstitialName) {
      print('$ad onAdDismissedFullScreenContent.');
      name.dispose();
      _createInterstitialAd();
      _showInterstitialAd();
    },
    onAdFailedToShowFullScreenContent: (InterstitialAd name, AdError error) {
      print('$ad onAdFailedToShowFullScreenContent: $error');
      name.dispose();
      _createInterstitialAd();
    },
  );
  _interstitialAd!.show();
  _interstitialName = null;
}

void _createInterstitialAd() {
  try{
InterstitialAd.load(
      adUnitId: "ca-app-pub-xxxyyyxxx*",
      request: request,
      adLoadCallback: InterstitialAdLoadCallback(
        onAdLoaded: (InterstitialAd name) {
          print('$ad loaded');
          _interstitialName = name;
          _numInterstitialLoadAttempts = 0;
          _interstitialAd!.setImmersiveMode(true);
        },
        onAdFailedToLoad: (LoadAdError error) {
          print('InterstitialAd failed to load: $error.');
          _numInterstitialLoadAttempts += 1;
          _interstitialName = null;
          if (_numInterstitialLoadAttempts <= maxFailedLoadAttempts) {
            _createInterstitialAd();
          }
        },
      ));
  }catch(error){
    print("An error occurred while loading the ad.");
    print("Error: ${error.toString()}");
  }
}
*="ca-app-pub-xxxyyyxxx" normally contains my interstitial ad number
1 Answers

You can look up error codes here.

In your case, code 1 is "invalid request"

public static final int ERROR_CODE_INVALID_REQUEST The ad request was invalid; for instance, the ad unit ID was incorrect. Constant Value: 1

Most likely you've used an incorrect ID.

Related