Flutter-获取唯一的设备ID [英] Flutter - Get Unique Device ID
问题描述
我正在使用基于令牌的系统制作一个应用程序.因此,用户可以购买令牌并使用令牌进行一些操作. 使用电子邮件和密码创建帐户后,每个用户均可免费获得10个令牌(作为试用版).
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.
我想通过每次获取一个新帐户来防止该用户再获得10个令牌,我想知道Android和iOS设备是否都具有唯一的设备ID?
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?
我可以为此目的使用它吗? https://pub.dartlang.org/packages/device_id#-example-tab -
Can I use this for that purpose? https://pub.dartlang.org/packages/device_id#-example-tab-
推荐答案
使用开发的 device_info 插件由Flutter团队本身负责.这是您在两个平台上都可以获取ID的方法.
Use device_info plugin developed by Flutter team itself. This is how you can get IDs on both platform.
在您的pubspec.yaml
文件中添加以下内容:
In your pubspec.yaml
file add this:
dependencies:
device_info: any
创建方法:
Future<String> _getId() async {
DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
if (Theme.of(context).platform == TargetPlatform.iOS) {
IosDeviceInfo iosDeviceInfo = await deviceInfo.iosInfo;
return iosDeviceInfo.identifierForVendor; // unique ID on iOS
} else {
AndroidDeviceInfo androidDeviceInfo = await deviceInfo.androidInfo;
return androidDeviceInfo.androidId; // unique ID on Android
}
}
使用方式如下:
Use it like:
String deviceId = await _getId();
或
_getId().then((id) {
String deviceId = id;
});
这篇关于Flutter-获取唯一的设备ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!