Outline
In this blog post, I will show you how to use device_info_plus
to get the information about the device such as the device name, device model name, device system name, etc.
- device_info_plus: https://pub.dev/packages/device_info_plus
You can see the full source code of this blog post at the following link.
Install device_info_plus
To check how to use device_info_plus in Flutter, execute the following command to create a new Flutter project.
flutter create device_info_plus_example
And the, execute the following command to install the device_info_plus
package.
flutter pub add device_info_plus
Next, let’s see how to use device_info_plus.
How to use
You can use device_info_plus
to get the following info.
import 'package:device_info_plus/device_info_plus.dart';
DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
// Android
AndroidDeviceInfo info = await deviceInfo.androidInfo;
print(info.version);
print(info.board);
print(info.bootloader);
print(info.brand);
print(info.device);
...
// iOS
IosDeviceInfo info = await deviceInfo.iosInfo;
print(info.name);
print(info.systemName);
print(info.systemVersion);
print(info.model);
print(info.localizedModel);
...
// Linux
LinuxDeviceInfo info = await deviceInfo.linuxInfo;
print(info.name);
print(info.version);
print(info.id);
print(info.versionCodename);
print(info.versionId);
...
// macOS
MacOsDeviceInfo info = await deviceInfo.macOsInfo;
print(info.computerName);
print(info.hostName);
print(info.arch);
print(info.model);
print(info.kernelVersion);
...
// Windows
WindowsDeviceInfo info = await deviceInfo.windowsInfo;
print(info.computerName);
print(info.numberOfCores);
print(info.systemMemoryInMegabytes);
The information is provided differently depending on the platform, so you should check the provided info before using it.
Also, you can get the common information like the following.
final info = await deviceInfo.deviceInfo;
print(info.toMap());
Example
To check how to use device_info_plus
, open the lib/main.dart
file and modify it like the below.
// ignore_for_file: avoid_print
import 'dart:io';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter/material.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
if (Platform.isAndroid) {
AndroidDeviceInfo info = await deviceInfo.androidInfo;
print(info.toMap());
} else if (Platform.isIOS) {
IosDeviceInfo info = await deviceInfo.iosInfo;
print(info.toMap());
} else if (Platform.isLinux) {
LinuxDeviceInfo info = await deviceInfo.linuxInfo;
print(info.toMap());
} else if (Platform.isMacOS) {
MacOsDeviceInfo info = await deviceInfo.macOsInfo;
print(info.toMap());
} else if (Platform.isWindows) {
WindowsDeviceInfo info = await deviceInfo.windowsInfo;
print(info.toMap());
}
final info = await deviceInfo.deviceInfo;
print(info.toMap());
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Device Info Plus'),
),
body: const Center(
child: Text(
'Example App',
),
),
);
}
}
When the app is started, the main
function is executed, and gets the device information from device_info_plus
.
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
if (Platform.isAndroid) {
AndroidDeviceInfo info = await deviceInfo.androidInfo;
print(info.toMap());
} else if (Platform.isIOS) {
IosDeviceInfo info = await deviceInfo.iosInfo;
print(info.toMap());
} else if (Platform.isLinux) {
LinuxDeviceInfo info = await deviceInfo.linuxInfo;
print(info.toMap());
} else if (Platform.isMacOS) {
MacOsDeviceInfo info = await deviceInfo.macOsInfo;
print(info.toMap());
} else if (Platform.isWindows) {
WindowsDeviceInfo info = await deviceInfo.windowsInfo;
print(info.toMap());
}
final info = await deviceInfo.deviceInfo;
print(info.toMap());
runApp(const MyApp());
}
If you get the information successfully, you can see the following result.
{name: iPhone 13 Pro Max, model: iPhone, systemName: iOS, utsname: {release: 21.3.0, version: Darwin Kernel Version 21.3.0: Wed Jan 5 21:37:58 PST 2022; root:xnu-8019.80.24~20/RELEASE_X86_64, machine: x86_64, sysname: Darwin, nodename: F200304NT01.local}, systemVersion: 15.2, localizedModel: iPhone, identifierForVendor: CED17DF0-20A2-4028-94C3-B4018E00DB92, isPhysicalDevice: false}
Completed
Done! we’ve seen how to use device_info_plus
to get the device information such as the device name, device model name, device system name, etc. Now, you can know which device your app is running on for debugging.
Was my blog helpful? Please leave a comment at the bottom. it will be a great help to me!
App promotion
Deku
.Deku
created the applications with Flutter.If you have interested, please try to download them for free.