Closed husen-hn closed 2 years ago
I'm having the same issue, tho I just followed the instructions from README.md
Apparently, you need to refresh the view once the ad is loaded. I fixed it by using the following:
// ...
listener: NativeAdListener(
onAdLoaded: (Ad ad) {
isAdReady = true;
}
)
//...
FutureBuilder(
builder: (context, snapshot) {
return isAdReady
? Container(child: AdWidget(ad: nativeAd))
: Center(child: CircularProgressIndicator());
},
future: Future(() async {
return Future.doWhile(() =>
Future.delayed(Duration(milliseconds: 10)).then((_) => !isAdReady)
);
}),
)
Hi @husen-hn, Sorry for the delayed response. I ran the official sample app and it builds and runs fine for me could you please try running that example app.
Hey According to the error displayed in the NativeAd view, you are rendering the same ad multiple time. which causes this error. if you are using this native ad in a ListView, you need to add AutomaticKeepAliveClientMixin
here's how to do it.
class NativeInlineAd extends StatefulWidget {
const NativeInlineAd();
@override
State createState() => _NativeInlineAdState();
}
class _NativeInlineAdState extends State<NativeInlineAd>
with AutomaticKeepAliveClientMixin {
// COMPLETE: Add NativeAd instance
late NativeAd _ad;
// COMPLETE: Add _isAdLoaded
bool _isAdLoaded = false;
@override
void initState() {
super.initState();
// COMPLETE: Create a NativeAd instance
_ad = NativeAd(
adUnitId: kReleaseMode
? '<YOUR_NATIVE_ADMOB_ID>'
: '<NATIVE_TEST_AD_ID>',
factoryId: 'googleNativeAdsCard',
request: const AdRequest(),
listener: NativeAdListener(
onAdLoaded: (_) {
setState(() {
_isAdLoaded = true;
});
},
onAdFailedToLoad: (ad, error) {
// Releases an ad resource when it fails to load
ad.dispose();
throw 'Ad load failed (code=${error.code} message=${error.message})';
},
),
);
// COMPLETE: Load an ad
_ad.load();
}
@override
Widget build(BuildContext context) {
super.build(context);
if (_isAdLoaded) {
return Container(
child: SizedBox(height: 200, child: AdWidget(ad: _ad)),
),
height: 270,
margin: const EdgeInsets.only(
left: 10,
right: 10,
top: 5,
),
padding: const EdgeInsets.all(8),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(
ValuesConstant.borderRadius,
),
border: Border.all(
width: 1,
color: Colors.black,
),
color: Theme.of(context).cardTheme.color,
),
alignment: Alignment.center,
);
}
return const SizedBox(
height: 72.0,
child: Center(
child: CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation<Color>(
Colors.grey,
),
),
),
);
}
@override
void dispose() {
_ad.dispose();
super.dispose();
}
@override
bool get wantKeepAlive => true;
}
Then You can easily include the above widget in the list view as any other widget.
let me know if t works.
NativeAd loaded successfully but not displayed ads on the flutter app.
Log
MainActivity
```java import androidx.annotation.NonNull; import io.flutter.embedding.android.FlutterActivity; import io.flutter.embedding.engine.FlutterEngine; import io.flutter.plugins.googlemobileads.GoogleMobileAdsPlugin; public class MainActivity extends FlutterActivity { @Override public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) { super.configureFlutterEngine(flutterEngine); GoogleMobileAdsPlugin.registerNativeAdFactory(flutterEngine, "listTile", new ListTileNativeAdFactory(getContext())); } @Override public void cleanUpFlutterEngine(@NonNull FlutterEngine flutterEngine) { super.cleanUpFlutterEngine(flutterEngine); GoogleMobileAdsPlugin.unregisterNativeAdFactory(flutterEngine, "listTile"); } } ```ListTileNativeAdFactory
```java import com.google.android.gms.ads.nativead.NativeAd; import com.google.android.gms.ads.nativead.NativeAdView; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.widget.ImageView; import android.widget.TextView; import java.util.Map; import io.flutter.plugins.googlemobileads.GoogleMobileAdsPlugin; class ListTileNativeAdFactory implements GoogleMobileAdsPlugin.NativeAdFactory { private final Context context; ListTileNativeAdFactory(Context context) { this.context = context; } @Override public NativeAdView createNativeAd( NativeAd nativeAd, Maplist_tile_native_ad.xml
```xmlPlugin Version
google_mobile_ads: ^0.13.5
Steps to Reproduce
I followed exactly the same steps described here
Actual results:
Logs
flutter run --verbose ``` [ +7 ms] Stopping app 'app.apk' on Google Pixel 3a Android 10 API 29. [ +1 ms] executing: /home/husen/Android/Sdk/platform-tools/adb -s 192.168.56.106:5555 shell am force-stop com.smes.smesofficial [ +305 ms] executing: /home/husen/Android/Sdk/platform-tools/adb -s 192.168.56.106:5555 shell pm list packages com.smes.smesofficial [ +75 ms] package:com.smes.smesofficial [ +2 ms] executing: /home/husen/Android/Sdk/platform-tools/adb -s 192.168.56.106:5555 shell cat /data/local/tmp/sky.com.smes.smesofficial.sha1 [ +40 ms] 8f214c20b708535155ea72b5810da38766fa7d3c [ ] Latest build already installed. [ ] executing: /home/husen/Android/Sdk/platform-tools/adb -s 192.168.56.106:5555 shell -x logcat -v time -t 1 [ +42 ms] --------- beginning of system 10-07 09:19:35.834 D/ConnectivityService( 678): releasing NetworkRequest [ TRACK_DEFAULT id=53, [ Capabilities: INTERNET&NOT_RESTRICTED&TRUSTED Uid: 10144] ] (release request) [ +30 ms] executing: /home/husen/Android/Sdk/platform-tools/adb -s 192.168.56.106:5555 shell am start -a android.intent.action.RUN -f 0x20000000 --ez enable-background-compilation true --ez enable-dart-profiling true --ez enable-checked-mode true --ez verify-entry-points true com.smes.smesofficial/com.smes.smesofficial.MainActivity [ +68 ms] Starting: Intent { act=android.intent.action.RUN flg=0x20000000 cmp=com.smes.smesofficial/.MainActivity (has extras) } [ ] Waiting for observatory port to be available... [+1821 ms] Observatory URL on device: http://127.0.0.1:43702/b1CseFunpXc=/ [ +1 ms] executing: /home/husen/Android/Sdk/platform-tools/adb -s 192.168.56.106:5555 forward tcp:0 tcp:43702 [ +20 ms] W/FlutterActivityAndFragmentDelegate( 7662): A splash screen was provided to Flutter, but this is deprecated. See flutter.dev/go/android-splash-migration for migration steps. [ +6 ms] 37665 [ ] Forwarded host port 37665 to device port 43702 for Observatory [ +7 ms] Caching compiled dill [ +715 ms] Connecting to service protocol: http://127.0.0.1:37665/b1CseFunpXc=/ [ +454 ms] Launching a Dart Developer Service (DDS) instance at http://127.0.0.1:0, connecting to VM service at http://127.0.0.1:37665/b1CseFunpXc=/. [ +485 ms] DDS is listening at http://127.0.0.1:36693/84fnu4RcJMY=/. [ +67 ms] Successfully connected to service protocol: http://127.0.0.1:37665/b1CseFunpXc=/ [ +70 ms] DevFS: Creating new filesystem on the device (null) [ +18 ms] W/es.smesofficia( 7662): Accessing hidden field Ljava/net/Socket;->impl:Ljava/net/SocketImpl; (greylist, reflection, allowed) [ ] W/es.smesofficia( 7662): Accessing hidden method Ldalvik/system/CloseGuard;->get()Ldalvik/system/CloseGuard; (greylist,core-platform-api, linking, allowed) [ +1 ms] W/es.smesofficia( 7662): Accessing hidden method Ldalvik/system/CloseGuard;->open(Ljava/lang/String;)V (greylist,core-platform-api, linking, allowed) [ +12 ms] DevFS: Created new filesystem on the device (file:///data/user/0/com.smes.smesofficial/code_cache/smesPBGUKE/smes/) [ +3 ms] Updating assets [ +235 ms] Manifest contained wildcard assets. Inserting missing file into build graph to force rerun. for more information see #56466. [ +9 ms] Syncing files to device Google Pixel 3a Android 10 API 29... [ +2 ms] <- reset [ ] Compiling dart to kernel with 0 updated files [ +2 ms] <- recompile package:smes/main.dart e437aaa6-d48b-4fc1-80cf-8d7af56d40b6 [ ] <- e437aaa6-d48b-4fc1-80cf-8d7af56d40b6 [ +4 ms] I/FLTFireMsgService( 7662): FlutterFirebaseMessagingBackgroundService started! [ ] D/ ( 7662): HostConnection::get() New Host Connection established 0xc9956b40, tid 7754 [ ] D/ ( 7662): HostComposition ext GL_OES_EGL_image_external_essl3 GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_gles_max_version_3_0 [ ] D/EGL_emulation( 7662): eglCreateContext: 0xbea00d20: maj 3 min 0 rcv 3 [ ] D/skia ( 7662): Shader compilation error [ ] D/skia ( 7662): ------------------------ [ ] D/skia ( 7662): Errors: [ ] D/skia ( 7662): [ ] D/skia ( 7662): Shader compilation error [ ] D/skia ( 7662): ------------------------ [ ] D/skia ( 7662): Errors: [ ] D/skia ( 7662): [ ] D/skia ( 7662): Shader compilation error [ ] D/skia ( 7662): ------------------------ [ ] D/skia ( 7662): Errors: [ ] D/skia ( 7662): [ ] D/skia ( 7662): Shader compilation error [ ] D/skia ( 7662): ------------------------ [ ] D/skia ( 7662): Errors: [ ] D/skia ( 7662): [ ] D/skia ( 7662): Shader compilation error [ ] D/skia ( 7662): ------------------------ [ ] D/skia ( 7662): Errors: [ ] D/skia ( 7662): [ ] D/skia ( 7662): Shader compilation error [ ] D/skia ( 7662): ------------------------ [ ] D/skia ( 7662): Errors: [ ] D/skia ( 7662): [ ] D/skia ( 7662): Shader compilation error [ ] D/skia ( 7662): ------------------------ [ ] D/skia ( 7662): Errors: [ ] D/skia ( 7662): [ ] D/skia ( 7662): Shader compilation error [ ] D/skia ( 7662): ------------------------ [ ] D/skia ( 7662): Errors: [ ] D/skia ( 7662): [ +7 ms] D/skia ( 7662): Shader compilation error [ ] D/skia ( 7662): ------------------------ [ ] D/skia ( 7662): Errors: [ ] D/skia ( 7662): [ +8 ms] D/skia ( 7662): Shader compilation error [ ] D/skia ( 7662): ------------------------ [ ] D/skia ( 7662): Errors: [ ] D/skia ( 7662): [ +13 ms] D/skia ( 7662): Shader compilation error [ ] D/skia ( 7662): ------------------------ [ ] D/skia ( 7662): Errors: [ ] D/skia ( 7662): [ +11 ms] D/skia ( 7662): Shader compilation error [ ] D/skia ( 7662): ------------------------ [ ] D/skia ( 7662): Errors: [ ] D/skia ( 7662): [ +14 ms] D/skia ( 7662): Shader compilation error [ ] D/skia ( 7662): ------------------------ [ ] D/skia ( 7662): Errors: [ ] D/skia ( 7662): [ +15 ms] D/skia ( 7662): Shader compilation error [ ] D/skia ( 7662): ------------------------ [ ] D/skia ( 7662): Errors: [ ] D/skia ( 7662): [ +24 ms] W/es.smesofficia( 7662): Accessing hidden method Ljava/security/spec/ECParameterSpec;->setCurveName(Ljava/lang/String;)V (greylist, reflection, allowed) [ +26 ms] W/es.smesofficia( 7662): Accessing hidden method Ldalvik/system/BlockGuard;->getThreadPolicy()Ldalvik/system/BlockGuard$Policy; (greylist,core-platform-api, linking, allowed) [ ] W/es.smesofficia( 7662): Accessing hidden method Ldalvik/system/BlockGuard$Policy;->onNetwork()V (greylist, linking, allowed) [ +29 ms] Updating files. [ ] DevFS: Sync finished [ +1 ms] Syncing files to device Google Pixel 3a Android 10 API 29... (completed in 169ms) [ ] Synced 0.0MB. [ +1 ms] <- accept [ +3 ms] Connected to _flutterView/0xca9d7810. [ ] Connected to _flutterView/0xcaa0b810. [ +3 ms] Flutter run key commands. [ +1 ms] r Hot reload. 🔥🔥🔥 [ ] R Hot restart. [ ] h List all available interactive commands. [ ] d Detach (terminate "flutter run" but leave application running). [ ] c Clear the screen [ ] q Quit (terminate the application on the device). [ ] Running with unsound null safety [ ] For more information see https://dart.dev/null-safety/unsound-null-safety [ ] An Observatory debugger and profiler on Google Pixel 3a Android 10 API 29 is available at: http://127.0.0.1:36693/84fnu4RcJMY=/ [ +99 ms] W/es.smesofficia( 7662): Accessing hidden method Lsun/misc/Unsafe;->getObject(Ljava/lang/Object;J)Ljava/lang/Object; (greylist, linking, allowed) [ +1 ms] W/es.smesofficia( 7662): Accessing hidden method Lsun/misc/Unsafe;->putInt(Ljava/lang/Object;JI)V (greylist, linking, allowed) [ +150 ms] D/skia ( 7662): Shader compilation error [ ] D/skia ( 7662): ------------------------ [ ] D/skia ( 7662): Errors: [ ] D/skia ( 7662): [ +49 ms] W/es.smesofficia( 7662): Accessing hidden method Lsun/misc/Unsafe;->putObject(Ljava/lang/Object;JLjava/lang/Object;)V (greylist, linking, allowed) [ +4 ms] W/es.smesofficia( 7662): Accessing hidden method Lsun/misc/Unsafe;->getLong(Ljava/lang/Object;J)J (greylist,core-platform-api, linking, allowed) [ +2 ms] W/es.smesofficia( 7662): Accessing hidden method Lsun/misc/Unsafe;->putInt(Ljava/lang/Object;JI)V (greylist, linking, allowed) [ +29 ms] W/es.smesofficia( 7662): Accessing hidden method Lsun/misc/Unsafe;->putInt(Ljava/lang/Object;JI)V (greylist, linking, allowed) [ ] E/flutter ( 7662): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: Failed assertion: boolean expression must not be null [ ] E/flutter ( 7662): #0 SplashController.setTheme (package:smes/screens/splash/logic/splash_controller.dart:47:9) [ ] E/flutter ( 7662):