How to get unique device id in flutter?
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?
Solution 1:
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.
Solution 2:
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: ^3.0.1
Create a method:
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 {
var androidDeviceInfo = await deviceInfo.androidInfo;
return androidDeviceInfo.androidId; // unique ID on Android
}
}
Usage:
String? deviceId = await _getId();
Solution 3:
I just published a plugin to provide a solution to your problem. It uses Settings.Secure.ANDROID_ID for Android and relies on identifierForVendor and the keychain for iOS to make the behaviour equivalent to Android's. Here's the link.
Solution 4:
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;
}