资源无法调用dispose-Flutter



我正在Flutter中测试移动广告,目前一切正常。但有时我会在调试控制台中看到这个错误。

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

此错误不会导致应用程序的操作出现问题,并且广告正在工作。但我想知道当我在应用程序中切换到发布模式时,它是否会返回到一个重要错误。

初始广告:

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();
}
}

广告使用:

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,
),
),
),
)),
],
),
);
}
}
Dispose是一个每当从有状态的小部件将从小部件树中永久删除。是的通常只在状态对象为被摧毁。Dispose释放分配给现有状态变量。

在您的情况下,状态已经删除,因此不会调用

在这种情况下,您只是在调用一个已经从上下文中删除的状态。请提供代码以供进一步解释。

最新更新