gpt4 book ai didi

flutter - testDeviceId 在 google-mobile-ads flutter 中不起作用

转载 作者:行者123 更新时间:2023-12-02 16:15:26 24 4
gpt4 key购买 nike

我试图用 requestConfiguration 设置我的测试设备 ID。为了检查测试设备的设置是否成功,我在 android studio 中使用 setTestDeviceIds 过滤了 logcat 但它显示了以下日志

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.

因此测试设备的设置不起作用,我无法使用真实广告进行测试。当我通过 testDeviceId 时,这个日志应该不会出现。您可以看到我在 RequestConfigurationbannerAd 中传递了 testDeviceId

重现步骤

这是我的 main.dart 文件

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



这是我的 adhelper.dart 文件

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 医生

运行 flutter doctor 并粘贴以下输出:

点击展开
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 依赖

运行 flutter pub deps -- --style=compact 并粘贴以下输出:

点击展开
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]


最佳答案

在我的例子中这是可行的

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

关于flutter - testDeviceId 在 google-mobile-ads flutter 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66881383/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com