Jan 17, 2023 iOS

How to get the Flutter Device name

Welcome to the LeadByCode .Today we are going to learn about How to get flutter device name as well as Device info.

In general, developing a mobile application is a difficult and time-consuming process. There are various frameworks available that provide excellent features for developing mobile applications. Android provides a native structural framework based on Java and Kotlin language for developing mobile applications, whereas iOS provides a system based on Objective-C/Swift language.
As a result, we require two distinct languages and structures in order to develop applications for both operating systems. Today, various frameworks have been introduced to help both operating systems and desktop applications overcome the complexity of structure. These frameworks are referred to as cross-platform development tools.

To get the device name in Flutter, you can use the device_info package. This package provides a DeviceI‌nfo class that can be used to retrieve various information about the device, including its name.

Here’s an example code snippet to retrieve the device name:

import 'package:device_info/device_info.dart';

void main() async {
  DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
  String deviceName;
  
  // Get device information
  if (Platform.isAndroid) {
    AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
    deviceName = androidInfo.model;
  } else if (Platform.isIOS) {
    IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
    deviceName = iosInfo.name;
  }

  print('Device Name: $deviceName');
}

In the code above, we first import the device_info package. We then create an instance of the DeviceInfoPlugin class. Next, we check if the current platform is Android or iOS using the Platform class provided by the Flutter framework.

If the platform is Android, we retrieve the device information using the androidInfo property of the DeviceInfoPlugin class and store the device name in the model property of the AndroidDeviceInfo class.

If the platform is iOS, we retrieve the device information using the iosInfo property of the DeviceInfoPlugin class and store the device name in the name property of the IosDeviceInfo class.

Finally, we print the device name to the console.