How to get unique device id in flutter?

Viewed 110322

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?

11 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: ^3.2.3

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 if(Platform.isAndroid) {
    var androidDeviceInfo = await deviceInfo.androidInfo;
    return androidDeviceInfo.androidId; // unique ID on Android
  }
}

Usage:

String? deviceId = await _getId();

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.

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;
}

Use device_id plugin

  1. Add in your following code in your .yaml file.

    device_id: ^0.1.3
    
  2. Add import in your class

    import 'package:device_id/device_id.dart';
    
  3. Now get device id from:

    String deviceid = await DeviceId.getID;
    

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.

  1. add to pubspec.yaml
  dependencies:
    ...
    client_information: ^1.0.1
  1. import to your project
import 'package:client_information/client_information.dart';
  1. then you can get device ID like this
/// Support on iOS, Android and web project
Future<String> getDeviceId() async {
  return (await ClientInformation.fetch()).deviceId;
}

Latest:

The plugin device_info has given deprecation notice and replaced by device_info_plus

Example:

dependencies:
  device_info_plus: ^2.1.0

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:

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;
  }
} 

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;
}

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:

  1. 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 the androidId to change when switiching networks. You can confirm this by yourself by changing androidDeviceInfo.id to androidDeviceInfo.androidId above.

  2. you can probably get away with using only androidDeviceInfo.name as it would not change ever.

  3. androidDeviceInfo.id can also change if OS is updated as it is an android os version.

  4. androidDeviceInfo.androidId should only be used if device uses fix mac address as mentioned in point 1. Otherwise, either use *.name only or androidDeviceInfo.id alongside with *.name.

androidID is removed since v4.1.0. Check the changelog.

android_id package is recommanded to get the correct androidId.

Related