How to get unique device id in flutter?

前端 未结 6 1640
没有蜡笔的小新
没有蜡笔的小新 2020-12-03 16:28

In Android we have, Settings.Secure.ANDROID_ID. I do not know the iOS equivalent. Is there a flutter plugin or a way to get a unique device id for both Android

6条回答
  •  时光取名叫无心
    2020-12-03 17:02

    There is a plugin called device_info. You can get it here. Check the official example here

     static Future> getDeviceDetails() async {
        String deviceName;
        String deviceVersion;
        String identifier;
        final DeviceInfoPlugin deviceInfoPlugin = new DeviceInfoPlugin();
        try {
          if (Platform.isAndroid) {
            var build = await deviceInfoPlugin.androidInfo;
            deviceName = build.model;
            deviceVersion = build.version.toString();
            identifier = build.androidId;  //UUID for Android
          } else if (Platform.isIOS) {
            var data = await deviceInfoPlugin.iosInfo;
            deviceName = data.name;
            deviceVersion = data.systemVersion;
            identifier = data.identifierForVendor;  //UUID for iOS
          }
        } on PlatformException {
          print('Failed to get platform version');
        }
    
    //if (!mounted) return;
    return [deviceName, deviceVersion, identifier];
    }
    

    You can store this UUID in the Keychain. This way you can set an unique ID for your device.

提交回复
热议问题