Home > Back-end >  Unique device id in flutter
Unique device id in flutter

Time:02-02

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.

CodePudding user response:

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

CodePudding user response:

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)

  • Related