我在Admob的小部件上遇到了问题。
我正在为一个Flutter应用程序开发一个新功能,其中包含一个Admob横幅小部件。但是当我设置另一个小部件的值时,Admob小部件会出现错误。
我正在使用:google_mobile_ads: ^0.11.0+1
横幅的构建方式如下:
@override
void initState() {
setState(() {
_adBanner = createBannerAd();
});
super.initState();
}
@override
void dispose() {
_adBanner.dispose();
super.dispose();
}
小部件的显示效果如下:
Container(
margin: EdgeInsets.only(bottom: myPercent(2, screenHeight)),
child: FutureBuilder(
future: _adBanner.load(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return Container(
margin: EdgeInsets.only(bottom: 3),
width: myPercent(95, screenWidth),
height: myPercent(6, screenHeight),
alignment: Alignment.center,
child: AdWidget(
ad: _adBanner,
),
);
}
return Container();
}),
日志错误捕获:
flutter: click
flutter: ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
flutter: The following assertion was thrown building AdWidget(dirty, state: _AdWidgetState#a1afb):
flutter: This AdWidget is already in the Widget tree
flutter: If you placed this AdWidget in a list, make sure you create a new instance in the builder function
flutter: with a unique ad object.
flutter: Make sure you are not using the same ad object in more than one AdWidget.
flutter:
flutter: The relevant error-causing widget was:
flutter: AdWidget file:///Users/sofian/Work/Personal/Mobile/WhatUDo/what_u_do/lib/views/idea.dart:295:34
flutter:
flutter: When the exception was thrown, this was the stack:
flutter: #0 _AdWidgetState.build (package:google_mobile_ads/src/ad_containers.dart:372:7)
flutter: #1 StatefulElement.build (package:flutter/src/widgets/framework.dart:4825:27)
flutter: #2 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4708:15)
flutter: #3 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4880:11)
flutter: #4 BuildOwner._runWithCurrentBuildTarget (package:flutter/src/widgets/framework.dart:2708:15)
flutter: #5 Element.rebuild (package:flutter/src/widgets/framework.dart:4407:12)
flutter: #6 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4687:5)
flutter: #7 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4871:11)
flutter: #8 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4682:5)
flutter: ... Normal element mounting (10 frames)
flutter: #18 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3660:14)
flutter: #19 Element.updateChild (package:flutter/src/widgets/framework.dart:3422:20)
flutter: #20 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4733:16)
flutter: #21 BuildOwner._runWithCurrentBuildTarget (package:flutter/src/widgets/framework.dart:2708:15)
flutter: #22 Element.rebuild (package:flutter/src/widgets/framework.dart:4407:12)
flutter: #23 StatelessElement.update (package:flutter/src/widgets/framework.dart:4789:5)
flutter: #24 Element.updateChild (package:flutter/src/widgets/framework.dart:3412:15)
flutter: #25 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4733:16)
flutter: #26 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4880:11)
flutter: #27 BuildOwner._runWithCurrentBuildTarget (package:flutter/src/widgets/framework.dart:2708:15)
flutter: #28 Element.rebuild (package:flutter/src/widgets/framework.dart:4407:12)
flutter: #29 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2813:33)
flutter: #30 WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:899:21)
flutter: #31 RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:320:5)
flutter: #32 SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1119:15)
flutter: #33 SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1057:9)
flutter: #34 SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:973:5)
flutter: #38 _invoke (dart:ui/hooks.dart:157:10)
flutter: #39 PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:253:5)
flutter: #40 _drawFrame (dart:ui/hooks.dart:120:31)
flutter: (elided 3 frames from dart:async)