[Flutter] Device Unique ID

2021-07-11 hit count image

Let's see how to get the user device unique ID by using the device_info_plus package in Flutter.

Outline

When we develop apps in Flutter, sometimes, we need to get the user device unique ID to distinguish the user. In this blog post, I will introduce how to use the device_info_plus package to get the user device unique ID in Flutter.

Install device_info_plus

To get the user device unique ID, we need to use the device_info_plus package. To use the device_info_plus package, execute the following command to install the device_info_plus package.

flutter pub add device_info_plus

Get device unique ID

Actuall, the device_info_plus package doesn’t a feature to get the device unique ID. So, we need to make a function to get the device unique ID with the device_info_plus package.

The following code is that I use to get the device unique ID.

Future<String> getDeviceUniqueId() async {
  var deviceIdentifier = 'unknown';
  var deviceInfo = DeviceInfoPlugin();

  if (Platform.isAndroid) {
    var androidInfo = await deviceInfo.androidInfo;
    deviceIdentifier = androidInfo.androidId!;
  } else if (Platform.isIOS) {
    var iosInfo = await deviceInfo.iosInfo;
    deviceIdentifier = iosInfo.identifierForVendor!;
  } else if (Platform.isLinux) {
    var linuxInfo = await deviceInfo.linuxInfo;
    deviceIdentifier = linuxInfo.machineId!;
  } else if (kIsWeb) {
    var webInfo = await deviceInfo.webBrowserInfo;
    deviceIdentifier = webInfo.vendor! +
        webInfo.userAgent! +
        webInfo.hardwareConcurrency.toString();
  }

  return deviceIdentifier;
}

If you use the function above, you can get the device unique ID not only on iOS and Android, but also on Linux and Web.

Completed

Done! we’ve seen how to use the device_info_plus package to get the user device unique ID in Flutter. Please try to use the code above to get the user device unique ID.

Was my blog helpful? Please leave a comment at the bottom. it will be a great help to me!

App promotion

You can use the applications that are created by this blog writer Deku.
Deku created the applications with Flutter.

If you have interested, please try to download them for free.

Posts