device_uid 2.0.0
device_uid: ^2.0.0 copied to clipboard
A Flutter plugin to retrieve device identifiers for advertising and analytics.
device_uid #
A Flutter plugin to retrieve device identifiers for advertising and analytics.
Features #
- Get a device-specific ID (
ANDROID_ID
on Android,identifierForVendor
on iOS). - Get the Advertising ID on both Android and iOS.
- Generate a random UUID.
- Null-safe and robust error handling.
Installation #
Add this to your pubspec.yaml
:
dependencies:
device_uid: ^2.0.0 # Use the latest version
Then run flutter pub get
.
Usage #
Here's how to use the different methods provided by the package:
import 'package:device_uid/device_uid.dart';
import 'package:flutter/material.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// Get the device-specific ID
String? deviceId = await DeviceUid.getDeviceId();
print('Device ID: $deviceId');
// Get the Advertising ID
String? adId = await DeviceUid.getAdId();
print('Advertising ID: $adId');
// Generate a random UUID
String? uuid = await DeviceUid.getUUID();
print('Generated UUID: $uuid');
}
Platform-Specific Configuration #
Android #
For apps targeting Android 13 (API 33) or higher, you must declare the AD_ID
permission in your AndroidManifest.xml
to access the Advertising ID. This plugin already includes this permission in its manifest, so it will be merged into your app's manifest automatically.
<uses-permission android:name="com.google.android.gms.permission.AD_ID"/>
iOS #
For apps running on iOS 14 or higher, you must request user permission to track their activity to get the Advertising ID. You can use a package like app_tracking_transparency
to request this permission before calling getAdId()
.
You also need to add the NSUserTrackingUsageDescription
key to your Info.plist
file:
<key>NSUserTrackingUsageDescription</key>
<string>This identifier will be used to deliver personalized ads to you.</string>
ID Types Explained #
Method | Android (getDeviceId() ) |
iOS (getDeviceId() ) |
Persistence |
---|---|---|---|
getDeviceId() |
Settings.Secure.ANDROID_ID . A 64-bit number that is randomly generated when the user first sets up the device. |
UIDevice.current.identifierForVendor . An alphanumeric string that uniquely identifies a device to the app’s vendor. |
The value may change if a factory reset is performed on the device. On Android 8.0+, the value is scoped by the app's signing key. |
getAdId() |
The Advertising ID provided by Google Play services. | The Advertising ID provided by the AdSupport framework. |
The user can reset this ID or opt out of personalized advertising, in which case the ID may be a string of zeros. |
getUUID() |
A randomly generated Version 4 UUID. | A randomly generated Version 4 UUID. | Not persistent. A new UUID is generated every time the method is called. |
☕ Buy Me a Coffee #
If you find this package useful, consider supporting me:
- Donate USDT (TRC 20):
TLbwVrZyaZujcTCXAb94t6k7BrvChVfxzi
Thank you for your support! ❤️