Home > Net >  Why flutter _getDeviceIdI always return null?
Why flutter _getDeviceIdI always return null?

Time:02-04

I am new to flutter.

I have this method

Future<String?> _getDeviceIdId() async 
{
    var deviceInfo = DeviceInfoPlugin();
    var androidDeviceInfo = await deviceInfo.androidInfo;
    return androidDeviceInfo.androidId; // unique ID on Android
}

Now androidDeviceInfo.androidId return a string.

However when I do

String? deviceId = await _getDeviceIdId();

deviceId is always null.

I tried this

String? deviceId;
    deviceId = await _getDeviceIdId().then((value) => deviceId);

but also no luck

CodePudding user response:

Device info package deprecated: for read

Please upgrade package to device_info_plus

CodePudding user response:

I use device info and package info packages and this code working perfectly in my projects.

Sample use: var deviceId = DeviceInfo.getInstance()?.deviceID ?? "";

class DeviceInfo {
  static int iosVersionCode = 1;

  static DeviceInfo? instance;
  String deviceID = "";
  String versionCode = "0";
  String version = "0";
  String model = "";
  String packageName = "";
  String osVersion = "";

  static DeviceInfo? getInstance() {
    if (instance == null) {
      instance = new DeviceInfo();
      return instance;
    } else {
      return instance;
    }
  }

  Future<void> deviceInfo() async {
    DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
    PackageInfo packageInfo = await PackageInfo.fromPlatform();

    if (Platform.isAndroid) {
      AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
      packageName = packageInfo.packageName;
      deviceID = androidInfo.androidId!;
      version = packageInfo.version;
      versionCode = packageInfo.buildNumber;
      model = androidInfo.model!;
      osVersion = "${androidInfo.version.sdkInt}" ;
    } else if (Platform.isIOS) {
      IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
      packageName = packageInfo.packageName;
      deviceID = iosInfo.identifierForVendor!;
      version = packageInfo.version;
      versionCode = version; //version is the unique things at the ios
      model = iosInfo.utsname.machine!;
      osVersion = iosInfo.systemVersion!;
    }
    debugPrint("Start---------------------------");
    debugPrint("packageName :$packageName");
    debugPrint("deviceID  :$deviceID");
    debugPrint("version  :$version");
    debugPrint("versionCode :$versionCode");
    debugPrint("model  :$model");
    debugPrint("osVersion  :$osVersion");
    debugPrint("End---------------------------");
  }
}

  •  Tags:  
  • Related