Home > other >  How to get android device version - Flutter
How to get android device version - Flutter

Time:09-11

I have been trying to get the Android device version (For example 11, 12). I have been trying to get only the number

This is what I have done till now

  void checkVersion(){
    print(Platform.operatingSystemVersion); // it prints "sdk_gphone64_arm64-userdebug 12 S2B2.211203.006 8015633 dev-keys"

   // I'm able to fetch the version number by splitting the string 
   // but the problem is that format of above string will vary by
   // operating system, so not suitable for parsing 

   int platformVersion = int.parse(Platform.operatingSystemVersion.split(' ')[1]); it prints '12'
  }

CodePudding user response:

Use the device_info_plus plugin and get Android, iOS, macOS, Linux versions with the following snippet:

  Future<String> _getOsVersion() async {
    final deviceInfo = DeviceInfoPlugin();
    if (Platform.isAndroid) {
      final info = await deviceInfo.androidInfo;
      return info.version.release ?? 'Unknown';
    }
    if (Platform.isIOS) {
      final info = await deviceInfo.iosInfo;
      return info.systemVersion ?? 'Unknown';
    }
    if (Platform.isMacOS) {
      final info = await deviceInfo.macOsInfo;
      return info.osRelease;
    }
    if (Platform.isLinux) {
      final info = await deviceInfo.linuxInfo;
      return info.version ?? 'Unknown';
    }
    return 'Unknown Version';
  }

CodePudding user response:

Try device_info_plus to get any device information you need.

Future<String?> getAndroidVersion() async {
  if (Platform.isAndroid) {
    DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
    AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
    return androidInfo.version.release;
  }
  throw UnsupportedError("Platform is not Android");
}

CodePudding user response:

You can use device_info_plus package to get the device version:

DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();
final androidInfo = await deviceInfoPlugin.androidInfo;
return androidInfo.version.sdkInt;

Or if you don't want to use any external plugin, you can use Platform.operatingSystemVersion. But it'll give you: "sdk_gphone64_arm64-userdebug 12 S2B2.211203.006 8015633 dev-keys"

So what you did is right. You've to split the string and get the device version:

final systemVerion = Platform.operatingSystemVersion;
int deviceVersion = int.parse(operatingSystemVersion.split(' ')[1]); 
print(deviceVersion);
//prints '12'
  • Related