— Flutter, Android, Mobile Development — 1 min read
Knowing the Android version of a user's device can be crucial for your Flutter app. This information helps you tailor the app's functionality or display specific features based on compatibility. Flutter offers two main approaches to achieve this:
1. Using the Platform
class (Limited Information):
The Platform
class provides basic platform information. While it can't directly access the Android version, it helps identify if the device is Android:
1import 'package:flutter/services.dart';2
3if (Platform.isAndroid) {4 // Code to handle Android specific logic5}
This approach is limited as it doesn't provide the actual version number.
2. Leveraging the device_info_plus
package (Recommended):
For more detailed information, including the Android version, using the device_info_plus
package is recommended. Here's how to do it:
pubspec.yaml
file:1dependencies:2 device_info_plus: ^4.2.2
1import 'package:device_info_plus/device_info_plus.dart';2
3Future<void> getAndroidVersion() async {4 final DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();5 final androidInfo = await deviceInfo.androidInfo;6 final version = androidInfo.version.release;7 print('Android Version: $version');8}
This code fetches the device information using the DeviceInfoPlugin
and extracts the Android version from the release
property.
Important Note:
While the androidInfo.version.sdkInt
property provides the API level (e.g., 33 for Android 13), parsing this integer into a human-readable version (e.g., 13.0) is not recommended by Flutter documentation due to potential inconsistencies. It's safer to use the release
property for the version string. Depending on your use case, you might want to use androidInfo.version.sdkInt
though, so it's handy to keep it in mind!