This tutorial shows you how to get the version of the platform or operating system that runs a Flutter application.
We already have a tutorial about how to detect the platform that runs a Flutter application. If you need to know what version of the operating system is used by a user, it can be done by using the device_info_plus
package too. Below are the examples.
Dependencies
To get the platform version easily, we can use the device_info_plus
package. Add the below dependency to your pubspec.yaml
file and run flutter pub get
.
device_info_plus: ^8.2.0
To use the package, add the import statement below.
import 'package:device_info_plus/device_info_plus.dart';
Then, you need to get the instance of DeviceInfoPlugin
final DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
Depending on which platform the application currently runs, you can get the version of the platform as shown in the examples below.
Get Android Version
First, you need to get the AndroidDeviceInfo
object from the androidInfo
getter of the DeviceInfoPlugin
object. Then, get the version
property whose type is AndroidBuildVersion
. It has the release
property which contains the major version of Android (such as 12, 13, 14). The SDK version can be read from the sdkInt
property.
final androidInfo = await deviceInfo.androidInfo;
String version = '${androidInfo.version.release} (SDK ${androidInfo.version.sdkInt})';
Get iOS Version
For iOS, you need to get the IosDeviceInfo
object from the iosInfo
getter of the DeviceInfoPlugin
object. Then, access the systemVersion
property.
final iosInfo = await deviceInfo.iosInfo;
String? version = iosInfo.systemVersion;
Get Linux Version
For Linux, you need to get the LinuxDeviceInfo
object from the linuxInfo
getter of the DeviceInfoPlugin
object. Then, access the version
property.
final linuxInfo = await deviceInfo.linuxInfo;
String? version = linuxInfo.version;
Get Windows Version
For Windows, you need to get the WindowsDeviceInfo
object from the windowsInfo
getter of the DeviceInfoPlugin
object. The WindowsDeviceInfo
object has a property named productName
which contains the value of HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProductName
registry key. However, the value is not always the actual version. If you want to get the correct version, use the value from the buildNumber
property. From the build number value, you can get the human readable version by creating a mapping of Windows build numbers.
final windowsInfo = await deviceInfo.windowsInfo;
String version = '${windowsInfo.productName} (Build ${windowsInfo.buildNumber})';
Get macOS Version
macOS version number consists of major, minor, and patch numbers with each part separated by dot. For example, if the version is 10.9.3, 10 is the major release number, 9 is the minor release number, and 3 is the update release number. You need to get the MacOsDeviceInfo
object from the macOsInfo
getter of the DeviceInfoPlugin
object. Then, access the majorVersion
, minorVersion
, and patchVersion
property.
final macOsInfo = await deviceInfo.macOsInfo;
String version = '${macOsInfo.majorVersion}.${macOsInfo.minorVersion}.${macOsInfo.patchVersion}';
Get Web Browser Version
If the Flutter application runs on a web browser, probably what you need to get is the browser version. You can get the WebBrowserInfo
object from the webBrowserInfo
getter of the DeviceInfoPlugin
object. Then, access the userAgent
property.
final webBrowserInfo = await deviceInfo.webBrowserInfo;
String? version = webBrowserInfo.userAgent;
Full Code
Below is a simple Flutter application that displays the version of the operating system (or the browser version for web) based on the current platform that runs the application.
import 'dart:io' show Platform;
import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: 'Woolha.com Flutter Tutorial',
home: GetVersionExample(),
);
}
}
class GetVersionExample extends StatefulWidget {
const GetVersionExample({Key? key}) : super(key: key);
@override
State<StatefulWidget> createState() {
return _GetVersionExampleState();
}
}
class _GetVersionExampleState extends State<GetVersionExample> {
String? _version;
@override
void initState() {
super.initState();
_getVersion();
}
void _getVersion() async {
final DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
String? version;
if (kIsWeb) {
final webBrowserInfo = await deviceInfo.webBrowserInfo;
version = webBrowserInfo.userAgent;
} else if (Platform.isAndroid) {
final androidInfo = await deviceInfo.androidInfo;
version = '${androidInfo.version.release} (SDK ${androidInfo.version.sdkInt})';
} else if (Platform.isIOS) {
final iosInfo = await deviceInfo.iosInfo;
version = iosInfo.systemVersion;
} else if (Platform.isLinux) {
final linuxInfo = await deviceInfo.linuxInfo;
version = linuxInfo.version;
} else if (Platform.isWindows) {
final windowsInfo = await deviceInfo.windowsInfo;
version = '${windowsInfo.productName} (Build ${windowsInfo.buildNumber})';
} else if (Platform.isMacOS) {
final macOsInfo = await deviceInfo.macOsInfo;
version = '${macOsInfo.majorVersion}.${macOsInfo.minorVersion}.${macOsInfo.patchVersion}';
}
setState(() {
_version = version;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Woolha.com Flutter Tutorial'),
backgroundColor: Colors.teal,
),
body: SizedBox(
width: double.infinity,
child: Center(
child: Text('Version: ${_version ?? '-'}'),
),
),
);
}
}
Summary
To get the version of the operating system or web browser, you can use the device_info_plus
package. You need to detect what is the platform that runs the application, then call the platform-specific code for getting the version of the platform.
You can also read about: