为什么`advertising_id` Flutter包给予的广告ID全为零?

rxztt3cl  于 2023-10-22  发布在  Flutter
关注(0)|答案(1)|浏览(120)

我试图在Android中使用Flutter获取Google Advertising ID。我将advertising_id包添加到我的项目中,并使用与此包包含的示例完全相同的代码:

String? advertisingId = await AdvertisingId.id(true);

但是当我在Android Studio中调试这段代码时,它总是返回00000000-0000-0000-0000-000000000000。我添加了以下一行来显示:

<uses-permission android:name="com.google.android.gms.permission.AD_ID"/>

但这并没有改变。
为什么广告ID总是归零?有没有办法把它弄到Flutter里?

eqzww0vc

eqzww0vc1#

不清楚你有没有放广告。id?

///do you use id here?
 String? _advertisingId = '**here your id**';
  bool? _isLimitAdTrackingEnabled;

  @override
  initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  initPlatformState() async {
    String? advertisingId;
    bool? isLimitAdTrackingEnabled;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      advertisingId = await AdvertisingId.id(true);
    } on PlatformException {
      advertisingId = 'Failed to get platform version.';
    }

    try {
      isLimitAdTrackingEnabled = await AdvertisingId.isLimitAdTrackingEnabled;
    } on PlatformException {
      isLimitAdTrackingEnabled = false;
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _advertisingId = advertisingId;
      _isLimitAdTrackingEnabled = isLimitAdTrackingEnabled;
    });
  }

相关问题