简体   繁体   中英

Flutter - Get Android device name

I am implementing an app using Flutter and I want to get the device name, ie X's Huawei phone, using device_info package.

I could get it in iOS using this code:

IosDeviceInfo iosDeviceInfo = await deviceInfo.iosInfo;
print('device name ${iosDeviceInfo.name}');

But in Android , I couldn't get the name. I can get many other values like the model, brand and display.

Is there a parameter in this package or another package that I can get Android device's name from it?

You can get Android device's name from board:

if(Platform.isAndroid){
  AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
  return androidInfo.board;
}

This is what I implemented, only tested for android and iOS. Essentially, iOS uses name, while Android uses manufacturer and model. In most cases I believe you can replace brand for manufacturer, I didn't have a lot of test devices, do your own research.

import 'package:device_info/device_info.dart';

String getDeviceName(){
    Map deviceInfo = (await DeviceInfoPlugin().deviceInfo).data;
    String? brand = deviceInfo['brand'];
    String? model = deviceInfo['model'];
    String? name = deviceInfo['name'];

    return name ?? '$manufacturer $model';
}

device_info package provides getting Android and iOS device info. It doesn't support any other platform as of now.

import 'package:device_info/device_info.dart';

DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
print('Running on ${androidInfo.model}');  // e.g. "Moto G (4)"

IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
print('Running on ${iosInfo.utsname.machine}');  // e.g. "iPod7,1"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM