How to get unique device id in flutter?

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

Leave a Comment