Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter - Get Unique Device ID

I am making an app with token based system. So users can buy tokens and using them they can make some actions. Each user gets 10 tokens for free (as a trial version) after creating an account using email and password.

I want to prevent that user gets another 10 tokens by getting a new account each time and I was wondering if there is something like unique device ID for both Android and iOS devices?

Can I use this for that purpose? https://pub.dartlang.org/packages/device_id#-example-tab-

like image 953
harunB10 Avatar asked Nov 29 '22 13:11

harunB10


1 Answers

Use device_info_plus plugin.

In your pubspec.yaml file add this

dependencies:
  device_info_plus: any

Create a method:

import 'dart:io';
import 'package:device_info_plus/device_info_plus.dart';
// ...

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

Use it like:

String? deviceId = await _getId();

or

_getId().then((id) {
  String? deviceId = id;
});
like image 178
CopsOnRoad Avatar answered Dec 04 '22 13:12

CopsOnRoad