Flutter实战—获取设备信息。
device_info:Get current device information from within the Flutter application.
版本查询 : https://pub.dev/packages/device_info
1.1 在项目的pubspec.yaml
文件中添加依赖:device_info
environment:
sdk: ">=2.1.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
//添加依赖
device_info: 0.4.0+2
1.2 安装下载依赖包
flutter packages get
如果需要在IOS运行,需要安装cocoapods和brew:
brew install cocoapods
pod setup
Flutter是全平台框架,可以在多种平台上运行,获取设备信息之前要先判断程序运行在哪种平台。
判断平台:通过dart.io
包下的PlatformPlatform
类来获取。
1.1 Platform类
class Platform {
...
static final bool isLinux = (_operatingSystem == "linux");
static final bool isMacOS = (_operatingSystem == "macos");
static final bool isWindows = (_operatingSystem == "windows");
static final bool isAndroid = (_operatingSystem == "android");
static final bool isIOS = (_operatingSystem == "ios");
static final bool isFuchsia = (_operatingSystem == "fuchsia");
...
}
1.2 示例
(1)导入dart.io
包
import 'dart:io';
(2)示例—判断平台(IOS/Android)
if(Platform.isIOS){
print('This is IOS.');
}else if(Platform.isAndroid)
print('This is Android.');
}
2.1导入device_info
包
import 'package:device_info/device_info.dart';
2.2 获取设备信息(IOS/Android)
void getDeviceInfo() async{
DeviceInfoPlugin deviceInfo = new DeviceInfoPlugin();
if(Platform.isIOS){
IosDeviceInfo iosDeviceInfo = await deviceInfo.iosInfo;
}else if(Platform.isAndroid){
AndroidDeviceInfo androidDeviceInfo = await deviceInfo.androidInfo;
}
}
2.3 IosDeviceInfo类
(1) 获取具体属性值:iosDeviceInfo.属性名(String name = iosDeviceInfo.name
)
class IosDeviceInfo {
...
final String name;
final String systemName;
final String systemVersion;
final String model;
final String localizedModel;
final String identifierForVendor;
final bool isPhysicalDevice;
final IosUtsname utsname;
...
}
2.4 AndroidDeviceInfo类
(1) 获取具体属性值:androidDeviceInfo.属性名(String brand = androidDeviceInfo.brand
)
class AndroidDeviceInfo {
...
final AndroidBuildVersion version;
final String board;
final String bootloader;
final String brand;
final String device;
final String display;
final String fingerprint;
final String hardware;
final String host;
final String id;
final String manufacturer;
final String model;
final String product;
final List<String> supported32BitAbis;
final List<String> supported64BitAbis;
final List<String> supportedAbis;
final String tags;
final String type;
final bool isPhysicalDevice;
final String androidId;
...
}
(2) AndroidBuildVersion类:androidDeviceInfo.version
class AndroidBuildVersion {
...
final String baseOS;
final String codename;
final String incremental;
final int previewSdkInt;
final String release;
final int sdkInt;
final String securityPatch;
...
}
发布此文,目的只是为学习到的知识点进行记录,如有错误请多多指点。