3

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

3 Answers 3

5

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';
  }
4

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");
}
3

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'
1
  • Note: although parsing it may returns the version number, this string may vary and according to Flutter docs it "is not suitable for parsing".
    – Ivanhercaz
    Commented Nov 2, 2023 at 1:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.