Home > Mobile >  A resource failed to call dispose - Flutter
A resource failed to call dispose - Flutter

Time:09-27

I'm testing mobile ads in Flutter, everything works fine for now. But sometimes I see this error in debug console.

W/System  (21924): A resource failed to call dispose.

This error does not cause problems in the operation of the application and the ads are working. But I want to know if it can return to an important error when I switch to the release mode in the application.

Interstitial ad:

import 'package:get/get.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';
import 'ad_helper.dart';

class AdController extends GetxController {
  InterstitialAd? interstitialAd;
  int adCounter = 0;
  int interstitialLoadAttempts = 0;
  int maxAttempts = 3;

  void showAd() {
    if (interstitialAd != null) {
      interstitialAd!.fullScreenContentCallback = FullScreenContentCallback(
        onAdDismissedFullScreenContent: (InterstitialAd ad) {
          ad.dispose;
          createAd();
        },
        onAdFailedToShowFullScreenContent: (InterstitialAd ad, AdError error) {
          ad.dispose;
          createAd();
          print('failed to show the ad $ad');
        },
      );
      if (adCounter % 15 == 0) {
        interstitialAd!.show();
        interstitialAd = null;
      }

      adCounter  ;
      print("adCounter:"   adCounter.toString());
    }
  }

  void createAd() {
    InterstitialAd.load(
      adUnitId: AdHelper.interstitialAdUnitId,
      request: const AdRequest(),
      adLoadCallback: InterstitialAdLoadCallback(
        onAdLoaded: (InterstitialAd ad) {
          interstitialAd = ad;
          interstitialLoadAttempts = 0;
          print('$ad loaded');
        },
        onAdFailedToLoad: (LoadAdError error) {
          interstitialLoadAttempts  = 1;
          interstitialAd = null;
          print('InterstitialAd failed to load: $error.');
          if (interstitialLoadAttempts < maxAttempts) {
            createAd();
          }
        },
      ),
    );
  }

  @override
  void onInit() {
    createAd();
    super.onInit();
  }

  @override
  void dispose() {
    interstitialAd?.dispose();
    super.dispose();
  }
}

Ad usage:

import 'package:ads_test_deneme/ads/ad_controller.dart';
import 'package:ads_test_deneme/page1_content.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class Page1 extends StatelessWidget {
  const Page1({super.key});

  @override
  Widget build(BuildContext context) {
    AdController adController = Get.put(AdController());

    return Scaffold(
      body: Column(
        children: [
          const Center(
            child: Text("Page1"),
          ),
          Center(
              child: GestureDetector(
            onTap: () {
              adController.showAd();

              Get.to(const Page1Content());
            },
            child: Container(
              height: 100,
              width: 200,
              color: Colors.blueAccent,
              child: const Text(
                "Content",
                style: TextStyle(
                  color: Colors.red,
                ),
              ),
            ),
          )),
        ],
      ),
    );
  }
}

CodePudding user response:

Dispose is a method triggered whenever the created object from the stateful widget is removed permanently from the widget tree. It is generally overridden and called only when the state object is destroyed. Dispose releases the memory allocated to the existing variables of the state.

In Your Case , the state already removed so its not calling

CodePudding user response:

In this case , you are simply calling a state which is already removed from the context. PLease provide code for further explanation.

  • Related