In Android we have, Settings.Secure.ANDROID_ID. I do not know the iOS equivalent.
Is there a flutter plugin or a way to get a unique device id for both Android and IOS in flutter?
-
7Does this unique ID be changed in any case or would remain same and never changed? If changed, what are the scenarios in which it would change?Ravi Garg– Ravi Garg2020-08-27 20:18:22 +00:00Commented Aug 27, 2020 at 20:18
-
8UPDATE: Because this thread has a very high Google ranking I wanted to mention here that the device_id package has been discontinued. The package platform_device_id looks like it works about the same way though, and has very recent activity. We're switching over to that after seeing errors in the log after iOS crashes which point to the old package.Adam Bellas– Adam Bellas2020-11-03 15:22:55 +00:00Commented Nov 3, 2020 at 15:22
-
On Android devices, there can be multiple users for the device. Is there a way to get a unique ID per user on the device?Jarl– Jarl2021-03-14 09:38:50 +00:00Commented Mar 14, 2021 at 9:38
-
@spycbanda did you find the answer of your question, I have the same question like yourswahyu– wahyu2022-01-21 03:24:19 +00:00Commented Jan 21, 2022 at 3:24
-
4I have been using device_info_plus for a while and came to realize the android Id is not unique.Users with similar devices have similar Ids.D. Ndungu– D. Ndungu2023-09-26 07:38:19 +00:00Commented Sep 26, 2023 at 7:38
15 Answers
Null safe code
Use device_info_plus plugin developed by Flutter community. This is how you can get IDs on both platform.
In your pubspec.yaml file add this:
dependencies:
device_info_plus: ^10.1.2
android_id: ^0.4.0
Create a method:
import 'package:android_id/android_id.dart';
import 'package:device_info_plus/device_info_plus.dart';
Future<String?> _getId() async {
var deviceInfo = DeviceInfoPlugin();
if (Platform.isIOS) { // import 'dart:io'
var iosDeviceInfo = await deviceInfo.iosInfo;
return iosDeviceInfo.identifierForVendor; // unique ID on iOS
} else if(Platform.isAndroid) {
var androidDeviceInfo = await deviceInfo.androidInfo;
return AndroidId().getId(); // unique ID on Android
}
}
Usage:
String? deviceId = await _getId();
14 Comments
There is a plugin called device_info. You can get it here.
Check the official example here
static Future<List<String>> getDeviceDetails() async {
String deviceName;
String deviceVersion;
String identifier;
final DeviceInfoPlugin deviceInfoPlugin = new DeviceInfoPlugin();
try {
if (Platform.isAndroid) {
var build = await deviceInfoPlugin.androidInfo;
deviceName = build.model;
deviceVersion = build.version.toString();
identifier = build.androidId; //UUID for Android
} else if (Platform.isIOS) {
var data = await deviceInfoPlugin.iosInfo;
deviceName = data.name;
deviceVersion = data.systemVersion;
identifier = data.identifierForVendor; //UUID for iOS
}
} on PlatformException {
print('Failed to get platform version');
}
//if (!mounted) return;
return [deviceName, deviceVersion, identifier];
}
You can store this UUID in the Keychain. This way you can set an unique ID for your device.
UPDATE
device_info is now device_info_plus
11 Comments
Update 1/3/2021: The recommended way is now the extended community plugin called device_info_plus. It supports more platforms than device_info and aims to support all that are supported by flutter. Here is an example usage:
import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:device_info_plus/device_info_plus.dart';
import 'dart:io';
Future<String> getDeviceIdentifier() async {
String deviceIdentifier = "unknown";
DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
if (Platform.isAndroid) {
AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
deviceIdentifier = androidInfo.androidId;
} else if (Platform.isIOS) {
IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
deviceIdentifier = iosInfo.identifierForVendor;
} else if (kIsWeb) {
// The web doesnt have a device UID, so use a combination fingerprint as an example
WebBrowserInfo webInfo = await deviceInfo.webBrowserInfo;
deviceIdentifier = webInfo.vendor + webInfo.userAgent + webInfo.hardwareConcurrency.toString();
} else if (Platform.isLinux) {
LinuxDeviceInfo linuxInfo = await deviceInfo.linuxInfo;
deviceIdentifier = linuxInfo.machineId;
}
return deviceIdentifier;
}
2 Comments
As of 2024, June, I started to use app_set_id package. It is compatible with latest dart and flutter versions.
As of 2022, December, last status about getting device id :
device_info_plus don't give unique device id anymore. So I started to use platform_device_id package.
I tested it on Android and it worked as same as device_info previously and provide the same id value. It also has a simple usage :
String deviceId = await PlatformDeviceId.getDeviceId;
This package uses updated android embedding version and also has null safety support.
3 Comments
platform_device_id is an old package, it is Dart 3 compatible and got me unique deviceId unlike device_info_plus. Thank you @cansu for mentioning itLatest:
The plugin device_info has given deprecation notice and replaced by device_info_plus
Example:
dependencies:
device_info_plus: ^9.0.2
How to use:
import 'package:device_info_plus/device_info_plus.dart';
DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
print('Running on ${androidInfo.model}'); // e.g. "Moto G (4)"
IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
print('Running on ${iosInfo.utsname.machine}'); // e.g. "iPod7,1"
WebBrowserInfo webBrowserInfo = await deviceInfo.webBrowserInfo;
print('Running on ${webBrowserInfo.userAgent}'); // e.g. "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:61.0) Gecko/20100101 Firefox/61.0"
You can check here full example:
For Unique ID:
You can use following code to get Unique ID:
String deviceIdentifier = '';
DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
if (kIsWeb) {
final WebBrowserInfo webInfo = await deviceInfo.webBrowserInfo;
deviceIdentifier = webInfo.vendor! +
webInfo.userAgent! +
webInfo.hardwareConcurrency.toString();
} else {
if (Platform.isAndroid) {
final AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
deviceIdentifier = androidInfo.id;
} else if (Platform.isIOS) {
final IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
deviceIdentifier = iosInfo.identifierForVendor!;
} else if (Platform.isLinux) {
final LinuxDeviceInfo linuxInfo = await deviceInfo.linuxInfo;
deviceIdentifier = linuxInfo.machineId!;
}
}
edit: There is no androidId on since v4.1.0.
5 Comments
iosInfo changes with each app upgrade/reinstall.hardwareConcurrency is not a good example. It will return the number of cores on the machine, not very unique..uuid packageUse device_id plugin
Add in your following code in your .yaml file.
device_id: ^0.1.3Add import in your class
import 'package:device_id/device_id.dart';Now get device id from:
String deviceid = await DeviceId.getID;
2 Comments
android.permission.READ_PHONE_STATE. And this will cause a warning while publishinh your app to Play Store as Apps using these permissions in an APK are required to have a privacy policy set.Use device_info_plus package developed by Flutter community. This is how you can get IDs on both platform.
In your pubspec.yaml file add this:
dependencies:
device_info_plus: ^3.2.3
Create a method:
Future<String> getUniqueDeviceId() async {
String uniqueDeviceId = '';
var deviceInfo = DeviceInfoPlugin();
if (Platform.isIOS) { // import 'dart:io'
var iosDeviceInfo = await deviceInfo.iosInfo;
uniqueDeviceId = '${iosDeviceInfo.name}:${iosDeviceInfo.identifierForVendor}'; // unique ID on iOS
} else if(Platform.isAndroid) {
var androidDeviceInfo = await deviceInfo.androidInfo;
uniqueDeviceId = '${androidDeviceInfo.name}:${androidDeviceInfo.id}' ; // unique ID on Android
}
return uniqueDeviceId;
}
Usage:
String deviceId = await getUniqueDeviceId();
Output:
M2102J20SG::SKQ1.211006.001
Note:
Do not use
androidDeviceInfo.androidId. This would change when your mac address changes. Mobile devices above Android OS 10/11 will generate a randomized MAC. This feature is enabled by default unless disabled manually. This would cause theandroidIdto change when switiching networks. You can confirm this by yourself by changingandroidDeviceInfo.idtoandroidDeviceInfo.androidIdabove.you can probably get away with using only
androidDeviceInfo.nameas it would not change ever.androidDeviceInfo.idcan also change if OS is updated as it is an android os version.androidDeviceInfo.androidIdshould only be used if device uses fix mac address as mentioned in point 1. Otherwise, either use*.nameonly orandroidDeviceInfo.idalongside with*.name.
Comments
androidID is removed since v4.1.0. Check the changelog.
android_id package is recommanded to get the correct androidId.
Comments
I release a new flutter plugin client_information might help. It provide a simple way to get some basic device information from your application user.
- add to pubspec.yaml
dependencies:
...
client_information: ^1.0.1
- import to your project
import 'package:client_information/client_information.dart';
- then you can get device ID like this
/// Support on iOS, Android and web project
Future<String> getDeviceId() async {
return (await ClientInformation.fetch()).deviceId;
}
2 Comments
Add the following code in your .yaml file.
device_info_plus: ^1.0.0
I used the following approach to get the device info that support in all platforms (i.e.) Android, IOS and Web.
import 'dart:io';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter/foundation.dart' show kIsWeb;
Future<String> getDeviceIdentifier() async {
String deviceIdentifier = "unknown";
DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
if (kIsWeb) {
WebBrowserInfo webInfo = await deviceInfo.webBrowserInfo;
deviceIdentifier = webInfo.vendor +
webInfo.userAgent +
webInfo.hardwareConcurrency.toString();
} else {
if (Platform.isAndroid) {
AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
deviceIdentifier = androidInfo.androidId;
} else if (Platform.isIOS) {
IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
deviceIdentifier = iosInfo.identifierForVendor;
} else if (Platform.isLinux) {
LinuxDeviceInfo linuxInfo = await deviceInfo.linuxInfo;
deviceIdentifier = linuxInfo.machineId;
}
}
return deviceIdentifier;
}
Comments
It's 2023, UUIDs are still hard apparently.
Use https://pub.dev/packages/app_set_id which uses the Android AppSetID and the iOS IDfV.
Each of android_id, device_info, device_info_plus have major issues, as described in other answers here.
1 Comment
android_id: ^0.1.3+1 Use this package but it only works on android.
device_info_plus: ^8.0.0 Use this package it works on IOS, Android, Web.
1 Comment
You can use this package: https://pub.dev/packages/flutter_udid
Plugin to retrieve a persistent UDID across app reinstalls on iOS, Android, Mac, Windows & Linux.
final String deviceId = await FlutterUdid.udid;
or
final String deviceId = await FlutterUdid.consistentUdid;
I have been using it for my projects, and it works great.
Comments
If you're serving ads you can use ASIdentifierManager. You should only use it for ads. There is no general UDID mechanism provided by the OS on iOS, for privacy reasons.
If you're using firebase_auth plugin you could signInAnonymously and then use the id of the FirebaseUser. This will give you an identifier that is specific to your Firebase app.