This tutorial shows you how to get the device model that runs a Flutter application.
Sometimes it's necessary to get the model of the device that runs your application. For example, if you want to display the device name to the users. If the application is developed using Flutter, you can see the examples in this tutorial. As for now, the examples in this tutorial only support getting the device model on Android, iOS, and macOS.
Dependencies
The package_info_plus
plugin can be used to get the device information. For iOS and macOS, there is a library named apple_product_name
which helps to translate the model number of Apple devices to their marketing names.
apple_product_name: ^1.4.0
package_info_plus: ^3.0.3
The above dependencies need to be added to your pubspec.yaml
file. Then, run flutter pub get
to install the packages.
To use the packages, add the following import statements on your file.
import 'package:apple_product_name/apple_product_name.dart';
import 'package:device_info_plus/device_info_plus.dart';
For the device_info_plus
plugin, you need to create an instance of DeviceInfoPlugin
first.
final DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
Get Android Device Model
For Android, you can get the AndroidDeviceInfo
object from the androidInfo
property of DeviceInfoPlugin
. The properties of AndroidDeviceInfo
that's related to device model information are:
manufacturer
: The manufacturer of the product/hardware. Example: Xiaomi.brand
: The consumer-visible brand of the product/hardware. Example: Poco.product
: The name of the overall product. Example: chaopin_id.model
: The end-user-visible name for the end product. Example: 21061110AG.
final androidInfo = await deviceInfo.androidInfo;
String deviceModel = '${androidInfo.manufacturer} ${androidInfo.brand} ${androidInfo.product} ${androidInfo.model}';
// Example: Xiaomi Poco chaopin_id 21061110AG
The returned device model is not always the same as the marketing name. To get the marketing name, you can use the mapping from the Android supported devices list.
Get iOS Device Model
For iOS, you can get the IosDeviceInfo
object from the iosInfo
property of DeviceInfoPlugin
. The IosDeviceInfo
class has a property named machine
which contains the device codes type a.k.a. machine ID. The apple_product_name
library adds an extension to the IosDeviceInfo
by adding productName
property whose value is the marketing name.
final iosInfo = await deviceInfo.iosInfo;
deviceModel = '${iosInfo.utsname.machine} (${iosInfo.utsname.productName})';
// Example: iPhone15,2 (Phone 14 Pro)
Get macOS Device Model
For getting the device model on macOS, you need to get the MacOsDeviceInfo
object from the macOsInfo
property of DeviceInfoPlugin
. The model
property of MacOsDeviceInfo
contains the model number of the device. Thanks to the extension created by apple_product_name
library, there is a property named productName
that returns the product name of the device.
final macOsInfo = await deviceInfo.macOsInfo;
deviceModel = '${macOsInfo.model} ${macOsInfo.productName}';
Summary
Getting the device model using Flutter can be done with the help of device_info_plus
plugin. However, it can only return the model number. For getting the marketing name of iOS devices, you can also use the apple_product_name
library. For Android, you need to map the model number from the Android supported devices list (see the link above).
You can also read about: