How to get a Unique Device id in Flutter?

· 3 min read
Unique Device id In Flutter
Unique Device id In Flutter

When we talk about a unique device id in native we are having Settings.Secure.ANDROID_ID to get a unique device id. So in this, we will be talking about How to get a Unique Device id in Flutter?

How to get unique device id for both Android and IOS in flutter?

There is a plugin called device_info. You can get it here.

  • In your pubspec.yaml file add this:
dependencies:
  device_info: ^0.4.0+4
  • Add import in your class.
    import 'package:device_id/device_id.dart';

The following code snippet shows how to get device id.

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

Users can store this UUID in the Keychain. This way you can set a unique ID for your device.

User can also create a function like the below:

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();

or

_getId().then((id) {
  String deviceId = id;
});

Conclusion:

In this article, we have been through How to get a Unique Device id in Flutter.

Still, need support for Flutter? We are always there to serve you better.

Keep Fluttering !!!
Keep Learning !!!

FlutterAgency.com is our portal Platform dedicated to Flutter Technology and Flutter Developers. The portal is full of cool resources from Flutter like Flutter Widget GuideFlutter ProjectsCode libs and etc.

FlutterAgency.com is one of the most popular online portal dedicated to Flutter Technology and daily thousands of unique visitors come to this portal to enhance their knowledge on Flutter.

1 comment

  1. Hi. When I plug my iPhone on MacBook e find my UDID on iTunes, the value is totally different between the UDID and the identifiForVendor wihch this library return for me.

    Can explane what’s the diference or whats the motive this happening?

Leave a Reply