Unique device id in flutter

5.6k Views Asked by At

I want to get a device id or any unique number that can not be changed when someone uninstalls the app. This device is for both android and iOS and is in the flutter framework. I want this to manage my device management.

Thanks for the help.

I tried some of the most commonly used packages in flutter like device_info_plus and flutter_udid but IDs generated from these changes every day.

4

There are 4 best solutions below

3
Rohan Jariwala On

Did you tried platform_device_id

This package can help you to get deviceId in android, iOS, windows, macOS and even in web and linux.

After importing the package you need to use following code

 try {
      deviceId = await PlatformDeviceId.getDeviceId;
    } on PlatformException {
      deviceId = 'Failed to get deviceId.';
    }
2
HoRiz On

You can use @Rohan's answer also. platform_device_id package. Read more


There is guide for device_info_plus package Read more

  • add package to your flutter project.

  • Create a method like this:

    Future<String> getUniqueDeviceId() async {
    String uniqueDeviceId = '';
    
    var deviceInfo = DeviceInfoPlugin();
    
    if (Platform.isIOS) { 
     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;
    
    }
    
  • use it like this:

    String deviceId = await getUniqueDeviceId();
    

Note (updateded):

Don't 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.

(Got details from @Ariel)

0
Kucing Malaya On

Use flutter_udid.

String deviceId = await FlutterUdid.udid;
0
Andrii Syrokomskyi On

To generate a unique ID for the device without collisions you can use this code

import 'package:id_gen/id_gen_helpers.dart';

final deviceId = genUuid;         // string
final deviceId = genUuid.hash();  // integer

from the lightweight and well-tested Dart package https://pub.dev/packages/id_gen