2

I was trying to set my test device id with the requestConfiguration. For checking whether setup for testdevice is successful, I filtered logcat in android studio with setTestDeviceIds but it's showing me the following log

2021-03-31 04:17:36.907 27185-27185/? I/Ads: Use RequestConfiguration.Builder().setTestDeviceIds(Arrays.asList("xxxxxxxxxxxxxxxxxxx")) to get test ads on this device.

So setting up for test device are not working and I can't test with real ads. As I'm passing testDeviceId's this log was supposed to not appear. You can see I'm passing testDeviceId's for both in RequestConfiguration and bannerAd.

Steps to reproduce

Here is my main.dart file

import 'package:flutter/material.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';
import 'package:provider/provider.dart';
import 'package:us/global/theme/app_themes.dart';
import 'package:us/utils/ad_helper.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();

  MobileAds.instance.initialize().then((InitializationStatus status) {
    print('Initialization done: ${status.adapterStatuses}');
    MobileAds.instance
        .updateRequestConfiguration(RequestConfiguration(
        testDeviceIds: AdHelper.allTestDevices));
  });


  runApp(
      MyApp()
  );
}


class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<ThemeChanger>(
      create: (_) => ThemeChanger(),
      child: new MaterialAppWithTheme(),
    );
  }
}

class MaterialAppWithTheme extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final theme = Provider.of<ThemeChanger>(context);

    return MaterialApp(
      title: 'Const',
      debugShowCheckedModeBanner: false,
      theme: theme.getDarkTheme ? ThemeData.dark():ThemeData.light(),
      home: HomeScreen(),
    );
  }
}

class HomeScreen extends StatefulWidget {
  @override
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {

  static final AdRequest request = AdRequest(
    testDevices: AdHelper.allTestDevices,
  );

  BannerAd _bannerAdd;
  bool _isBannerAdLoaded = false;

  Future<InitializationStatus> _initGoogleMobileAds() {
    // TODO: Initialize Google Mobile Ads SDK
    return MobileAds.instance.initialize();
  }

  @override
  void initState() {
    super.initState();


    _bannerAdd = BannerAd(
      adUnitId: AdHelper.homePageBannerAd,
      size: AdSize.banner,
      request: request,
      listener: AdListener(
        onAdLoaded: (_) {  // Future<SharedPrefData> _setDarkMode(bool darkMode) async{
          //   SharedPreferences prefs = await SharedPreferences.getInstance();
          //   log.i('Current dark mode  $darkMode.');
          //   await prefs.setBool('darkmode', darkMode);
          //   log.i('Dark mode toggled successfully');
          //   return await getPersistedData();
          // }
          setState(() {
            _isBannerAdLoaded = true;
          });
        },
        onAdFailedToLoad: (ad, error) {
          // Releases an ad resource when it fails to load
          ad.dispose();

          print('Ad load failed (code=${error.code} message=${error.message})');
        },
      ),
    );

    _bannerAdd.load();

  }
  

  @override
  Widget build(BuildContext context) {

    //this gonna give us total height and with of our device
    return Scaffold(
      // bottomNavigationBar: BottomNavBar(),
      body: Stack(
        children: <Widget>[
          SafeArea(
            child: Padding(
              padding: const EdgeInsets.symmetric(horizontal: 20),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  this._isBannerAdLoaded? Container(
                    margin: EdgeInsets.only(bottom: 35),
                    height: 72.0,
                    alignment: Alignment.center,
                    child: AdWidget(ad: _bannerAdd),
                  ):Text('')
                ],
              ),
            ),
          )
        ],
      ),
    );
  }
}



Here is my adhelper.dart file

import 'dart:io';

class AdHelper{

  static String TEST_BANNER_AD = "ca-app-pub-3940256099942544/6300978111";

 static String get homePageBannerAd{
   if(Platform.isAndroid){
     return TEST_BANNER_AD;
   }
   else if(Platform.isIOS){
     return TEST_BANNER_AD;
   }
   else
     throw new UnsupportedError("Platform is not supported..");
 }

 static List<String> get allTestDevices{

       String MY_REDMI_NOTE_9="xxxxxxxxxxx";


   return [MY_REDMI_NOTE_9];
  }

}

Flutter doctor

Run flutter doctor and paste the output below:

Click To Expand
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 2.0.1, on Linux, locale en_US.UTF-8)
[!] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
    ✗ Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/docs/get-started/install/linux#android-setup for more details.
[✓] Chrome - develop for the web
[✓] Android Studio (version 3.5)
[✓] IntelliJ IDEA Ultimate Edition (version 2020.2)
[✓] VS Code (version 1.54.3)
[✓] Connected device (2 available)

! Doctor found issues in 1 category.

Flutter dependencies

Run flutter pub deps -- --style=compact and paste the output below:

Click To Expand
Dart SDK 2.12.0
Flutter SDK 2.0.1
us_constitution 1.0.0+1

dependencies:
- dynamic_text_highlighting 2.2.0 [flutter]
- flutter 0.0.0 [characters collection meta typed_data vector_math sky_engine]
- flutter_svg 0.19.3 [flutter meta path_drawing vector_math xml]
- flutter_xlider 3.4.0 [flutter]
- google_mobile_ads 0.11.0+4 [meta flutter]
- logger 1.0.0
- path_provider 2.0.1 [flutter path_provider_platform_interface path_provider_macos path_provider_linux path_provider_windows]
- provider 5.0.0 [collection flutter nested]
- shared_preferences 2.0.5 [meta flutter shared_preferences_platform_interface shared_preferences_linux shared_preferences_macos shared_preferences_web shared_preferences_windows]
- sqflite 1.3.2+4 [flutter sqflite_common path]
- url_launcher 6.0.3 [flutter url_launcher_platform_interface url_launcher_linux url_launcher_macos url_launcher_windows url_launcher_web]

dev dependencies:
- flutter_test 0.0.0 [flutter test_api path fake_async clock stack_trace vector_math async boolean_selector characters charcode collection matcher meta source_span stream_channel string_scanner term_glyph typed_data]

transitive dependencies:
- async 2.5.0 [collection]
- boolean_selector 2.1.0 [source_span string_scanner]
- characters 1.1.0
- charcode 1.2.0
- clock 1.1.0
- collection 1.15.0
- convert 2.1.1 [charcode typed_data]
- fake_async 1.2.0 [clock collection]
- ffi 1.0.0
- file 6.1.0 [meta path]
- flutter_web_plugins 0.0.0 [flutter js characters collection meta typed_data vector_math]
- js 0.6.3
- matcher 0.12.10 [stack_trace]
- meta 1.3.0
- nested 1.0.0 [flutter]
- path 1.8.0
- path_drawing 0.4.1+1 [vector_math meta path_parsing flutter]
- path_parsing 0.1.4 [vector_math meta]
- path_provider_linux 2.0.0 [path xdg_directories path_provider_platform_interface flutter]
- path_provider_macos 2.0.0 [flutter]
- path_provider_platform_interface 2.0.0 [flutter meta platform plugin_platform_interface]
- path_provider_windows 2.0.0 [path_provider_platform_interface meta path flutter ffi win32]
- petitparser 3.1.0 [meta]
- platform 3.0.0
- plugin_platform_interface 1.0.3 [meta]
- process 4.1.0 [file path platform]
- shared_preferences_linux 2.0.0 [flutter file meta path path_provider_linux shared_preferences_platform_interface]
- shared_preferences_macos 2.0.0 [shared_preferences_platform_interface flutter]
- shared_preferences_platform_interface 2.0.0 [flutter]
- shared_preferences_web 2.0.0 [shared_preferences_platform_interface flutter flutter_web_plugins meta]
- shared_preferences_windows 2.0.0 [shared_preferences_platform_interface flutter file meta path path_provider_platform_interface path_provider_windows]
- sky_engine 0.0.99
- source_span 1.8.0 [charcode collection path term_glyph]
- sqflite_common 1.0.3+1 [synchronized path meta]
- stack_trace 1.10.0 [path]
- stream_channel 2.1.0 [async]
- string_scanner 1.1.0 [charcode source_span]
- synchronized 2.2.0+2
- term_glyph 1.2.0
- test_api 0.2.19 [async boolean_selector collection meta path source_span stack_trace stream_channel string_scanner term_glyph matcher]
- typed_data 1.3.0 [collection]
- url_launcher_linux 2.0.0 [flutter]
- url_launcher_macos 2.0.0 [flutter]
- url_launcher_platform_interface 2.0.1 [flutter plugin_platform_interface]
- url_launcher_web 2.0.0 [url_launcher_platform_interface meta flutter flutter_web_plugins]
- url_launcher_windows 2.0.0 [flutter]
- vector_math 2.1.0
- win32 2.0.0 [ffi]
- xdg_directories 0.2.0 [meta path process]
- xml 4.5.1 [collection convert meta petitparser]


2 Answers 2

8

In my case this works

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await MobileAds.instance.initialize().then((InitializationStatus status) {
    print('Initialization done: ${status.adapterStatuses}');
    MobileAds.instance.updateRequestConfiguration(
      RequestConfiguration(
          tagForChildDirectedTreatment:
              TagForChildDirectedTreatment.unspecified,
          testDeviceIds: <String>["list of test device ids seprated by ,(comas)"]),
    );
  });
  runApp(MyApp());
}
1

you can do it like this

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  MobileAds.instance.initialize();
  // thing to add
  RequestConfiguration configuration = RequestConfiguration(testDeviceIds: ['E46609DD331AF2754D38B3DCE3ED011B']);
  MobileAds.instance.updateRequestConfiguration(configuration);
  runApp(const MyApp());
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.