flutter_advertising_id 1.0.0 flutter_advertising_id: ^1.0.0 copied to clipboard
Flutter plugin for Android and IOS, a cross-platform to get Advertising id that is GAID for android and IDFA for ios. this plugin using kotlin android and swift ios for implement method channel.
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:flutter_advertising_id/flutter_advertising_id.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _advertisingId = 'Unknown';
@override
void initState() {
super.initState();
initPlatformState();
}
// AdvertisingId are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String advertisingId;
// Advertising id may fail, so we use a try/catch PlatformException.
try {
advertisingId = await FlutterAdvertisingId.advertisingId;
} on PlatformException {
advertisingId = 'Failed to get platform version.';
}
// 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;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Text('Running on: $_advertisingId\n'),
),
),
);
}
}