来源: Flutter获取Android/iOS设备信息 – 掘金
我们在进行各个系统的原生开发时,都有对应的方法获取设备信息,那么在使用Flutter时如何获取设备相关的相关信息呢?
我们本文就来介绍一个Flutter插件:
Flutter Device Info
下面我们来逐步介绍如何获取设备信息。
首先在工程的pubspec.yaml中添加依赖
dependencies:
device_info: ^0.4.0+1
复制代码
下载安装这个依赖包
在工程主目录下执行:
flutter packages get
复制代码
在代码中使用
首先我们引入device_info.dart:
import 'package:device_info/device_info.dart';
复制代码
iOS安装cocoapods
如果需要运行在iOS上运行,如果电脑上没有安装cocoapods,此时会报出异常:
Warning: CocoaPods not installed. Skipping pod install.
CocoaPods is used to retrieve the iOS platform side's plugin code that responds to your plugin usage on the Dart side.
Without resolving iOS dependencies with CocoaPods, plugins will not work on iOS.
For more info, see https://flutter.io/platform-plugins
To install:
brew install cocoapods
pod setup
复制代码
此时我们则需要安装并配置cocoapods(确保机器上已经安装好了brew):
brew install cocoapods
pod setup
复制代码
获取Android与iOS设备信息
void getDeviceInfo() async {
DeviceInfoPlugin deviceInfo = new DeviceInfoPlugin();
if(Platform.isAndroid) {
AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
print(_readAndroidBuildData(androidInfo).toString());
} else if (Platform.isIOS) {
IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
print(_readIosDeviceInfo(iosInfo).toString());
}
}
复制代码
构造设备信息的Map
为了更方便的打印和使用信息,我们将AndroidDeviceInfo和IosDeviceInfo构造成Map:
Map<String, dynamic> _readAndroidBuildData(AndroidDeviceInfo build) {
return <String, dynamic>{
'version.securityPatch': build.version.securityPatch,
'version.sdkInt': build.version.sdkInt,
'version.release': build.version.release,
'version.previewSdkInt': build.version.previewSdkInt,
'version.incremental': build.version.incremental,
'version.codename': build.version.codename,
'version.baseOS': build.version.baseOS,
'board': build.board,
'bootloader': build.bootloader,
'brand': build.brand,
'device': build.device,
'display': build.display,
'fingerprint': build.fingerprint,
'hardware': build.hardware,
'host': build.host,
'id': build.id,
'manufacturer': build.manufacturer,
'model': build.model,
'product': build.product,
'supported32BitAbis': build.supported32BitAbis,
'supported64BitAbis': build.supported64BitAbis,
'supportedAbis': build.supportedAbis,
'tags': build.tags,
'type': build.type,
'isPhysicalDevice': build.isPhysicalDevice,
'androidId': build.androidId
};
}
复制代码
iOS:
Map<String, dynamic> _readIosDeviceInfo(IosDeviceInfo data) {
return <String, dynamic>{
'name': data.name,
'systemName': data.systemName,
'systemVersion': data.systemVersion,
'model': data.model,
'localizedModel': data.localizedModel,
'identifierForVendor': data.identifierForVendor,
'isPhysicalDevice': data.isPhysicalDevice,
'utsname.sysname:': data.utsname.sysname,
'utsname.nodename:': data.utsname.nodename,
'utsname.release:': data.utsname.release,
'utsname.version:': data.utsname.version,
'utsname.machine:': data.utsname.machine,
};
}
复制代码
设备信息示例(基于模拟器):
Android:
{
version.securityPatch: 2018-09-05,
version.sdkInt: 28,
version.release: 9,
version.previewSdkInt: 0,
version.incremental: 5124027,
version.codename: REL,
version.baseOS: ,
board: goldfish_x86_64,
bootloader: unknown,
brand: google,
device: generic_x86_64,
display: PSR1.180720.075,
fingerprint: google/sdk_gphone_x86_64/generic_x86_64:9/PSR1.180720.075/5124027:user/release-keys,
hardware: ranchu,
host: abfarm730,
id: PSR1.180720.075,
manufacturer: Google,
model: Android SDK built for x86_64,
product: sdk_gphone_x86_64,
supported32BitAbis: [x86],
supported64BitAbis: [x86_64],
supportedAbis: [x86_64, x86],
tags: release-keys,
type: user,
isPhysicalDevice: false,
androidId: 998921b52c7a7b79
}
复制代码
iOS:
{
name: iPhone XR,
systemName: iOS,
systemVersion: 12.1,
model: iPhone,
localizedModel: iPhone,
identifierForVendor: 367F5936-39E1-4DFA-8DD2-9542424256BE,
isPhysicalDevice: false,
utsname.sysname:: Darwin,
utsname.nodename:: bogon,
utsname.release:: 18.2.0,
utsname.version:: Darwin Kernel Version 18.2.0: Thu Dec 20 20:46:53 PST 2018;
root:xnu-4903.241.1~1/RELEASE_X86_64,
utsname.machine:: x86_64
}
复制代码
获取屏幕宽高密度等信息
关于如何获取屏幕宽度高度和分辨率等信息,有需要的同学可以私信或者留言,我会发一篇
作者:我与bug不得不说的故事
链接:https://juejin.cn/post/6844903817025159175
来源:稀土掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。