Flutter获取Android/iOS设备信息

我们在进行各个系统的原生开发时,都有对应的方法获取设备信息,那么在使用Flutter时如何获取设备相关的相关信息呢?

我们本文就来介绍一个Flutter插件:
Flutter Device Info

下面我们来逐步介绍如何获取设备信息。

首先在工程的pubspec.yaml中添加依赖

1
2
dependencies:
device_info: ^0.4.0+1

下载安装这个依赖包

在工程主目录下执行:

1
flutter packages get

在代码中使用

首先我们引入device_info.dart:

1
import 'package:device_info/device_info.dart';

iOS安装cocoapods

如果需要运行在iOS上运行,如果电脑上没有安装cocoapods,此时会报出异常:

1
2
3
4
5
6
7
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):

1
2
brew install cocoapods
pod setup

获取Android与iOS设备信息

1
2
3
4
5
6
7
8
9
10
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:

Android:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
{
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
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
}

获取屏幕宽高密度等信息

关于如何获取屏幕宽度高度和分辨率等信息,可以参考这篇文章


爆款好课来袭,磊哥邀你一起学习!

AI充电季-Google开发专家带你系统掌握AI知识

整个课程以理论加实战为核心,从基础原理、代码案例、模型设计、训练流程、模型评估、参数调优、服务部署铺开,带你手把手入门深度学习,一次性系统掌握实践 AI 的所有知识和流程。

本课程共计665分钟,58节,如果每天学习1小时,预计学习12天。

course-csdn-google-ai-c300

零基础搞定Python数据分析与挖掘

基于灵巧的Python代码,可大幅度提高工作效率。轻松实现数据分析与挖掘的应用,并独立完成数据分析项目。

本课程已更新1183分钟,82节,如果每天学习1小时,预计学习20天。

course-csdn-python-c300

本文原创地址为:https://www.examplecode.cn/2019/04/10/flutter-device-info/
转载请注明出处。

我的博客中关于Flutter的所有文章可以点击这里找到,欢迎关注!

程序员磊哥 wechat