Learn Flutter Step by Step
Posted on
< Previous
Flutter device_info_plus package that provides information about the device the Flutter app is running on. This information can be useful for various purposes such as debugging or providing device-specific features.
Here are some key features of device_info_plus:
It can retrieve information about the device such as its operating system, model name, and unique ID.
It can be used on both Android and iOS devices.
It provides a simple and easy-to-use API for accessing device information.
Add this package in pubspec.yaml:
dependencies:
flutter:
sdk: flutter
device_info_plus: ^8.1.0
Here's an example of how to use device_info_plus to retrieve device information:
import 'package:device_info_plus/device_info_plus.dart';
Future getDeviceInformation() async {
final DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();
try {
if (Platform.isAndroid) {
final AndroidDeviceInfo androidDeviceInfo =
await deviceInfoPlugin.androidInfo;
print('Running on ${androidDeviceInfo.brand} ${androidDeviceInfo.model}');
} else if (Platform.isIOS) {
final IosDeviceInfo iosDeviceInfo = await deviceInfoPlugin.iosInfo;
print('Running on ${iosDeviceInfo.name} ${iosDeviceInfo.model}');
}
} catch (e) {
print('Error: $e');
}
}
In this example,
We first import the device_info_plus package. We then define an asynchronous function called getDeviceInformation() that retrieves and prints device information.
Inside the function, we create an instance of the DeviceInfoPlugin class, which is the main class of the package. We then use Platform.isAndroid and Platform.isIOS to check if the app is running on an Android or iOS device, respectively.
If the app is running on an Android device, we retrieve the device information using deviceInfoPlugin.androidInfo, which returns an instance of AndroidDeviceInfo. We then print the brand and model of the device.
If the app is running on an iOS device, we retrieve the device information using deviceInfoPlugin.iosInfo, which returns an instance of IosDeviceInfo. We then print the name and model of the device.
Finally, we catch any errors that might occur and print the error message.
This is just a basic example, but device_info_plus provides many other methods for retrieving information about the device. For example, you can use deviceInfoPlugin.androidId to retrieve the unique ID of an Android device, or deviceInfoPlugin.iosName to retrieve the name of an iOS device.
For more info about device_info_plus package visit https://pub.dev/packages/device_info_plus" target="_blank">Flutter Official pub.dev