How to Detect Device Model in Flutter
A mobile app might be running on various mobile devices with different configurations. Hence, it’s important for developers to get the device model of the users. Then only the developer can address device-specific bugs and issues.
Let’s check how to get device model in Flutter.
The device info is a Flutter package that gives information about the user device. You can install the package by adding device info dependency in the pubspec.yaml file.
dependencies:
flutter:
sdk: flutter
device_info: ^1.0.0
You can get the model name of the device by using the following snippet.
if (Platform.isAndroid) {
AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
print(androidInfo.model);
} else if (Platform.isIOS) {
IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
print(iosInfo.utsname.machine);
As you see, you get device information based on the platform.
Following is the complete example to detect device model in Flutter.
import 'package:flutter/material.dart';
import 'dart:io';
import 'package:device_info/device_info.dart';
void main() => runApp(App());
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Get Device Model Example',
home: FlutterExample(),
);
}
}
class FlutterExample extends StatefulWidget {
FlutterExample({Key key}) : super(key: key);
@override
_FlutterExampleState createState() => _FlutterExampleState();
}
class _FlutterExampleState extends State<FlutterExample> {
final DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
getModel() async {
if (Platform.isAndroid) {
AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
print(androidInfo.model);
} else if (Platform.isIOS) {
IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
print(iosInfo.utsname.machine);
}
}
@override
void initState() {
getModel();
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Flutter Device Model')),
body: Center(child: Text('Flutter Device Info Example')));
}
}
This gives output as given below.
I hope this Flutter tutorial will be helpful for you.