Flutter 获取iOS/Android设备信息

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

dependencies:
  device_info: ^0.4.0+1

下载安装这个依赖包

flutter packages get

首先我们引入device_info.dart:

import 'package:device_info/device_info.dart';

获取iOS与Android设备信息

根据手机设备进行判断
Platform.isAndroid  == Yes  才能调用方法

DeviceInfoPlugin deviceInfo = new DeviceInfoPlugin();
AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo; 
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());
  }
}

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:

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 所获取的信息

{
    name: iPhone XR, 
    systemName: iOS, 
    systemVersion: 13.1, 
    model: iPhone, 
    localizedModel: iPhone, 
    identifierForVendor: 367F5936-39E1-4DFA-8DD2-9542424256BE, 
    isPhysicalDevice: true, 
    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
}

Android 所获取的信息

{
 version.securityPatch: 2019-03-01,
 version.sdkInt: 26, 
 version.release: 8.0.0, 
 version.previewSdkInt: 0, 
 version.incremental: C7010ZCS3CSC1,               
 version.codename: REL, 
 version.baseOS:samsung/c7proltezc/c7proltechn:8.0.0/R16NW/C7010ZCU2CRJ1:user/release-keys,
  board: msm8953, 
  bootloader: C7010ZCS3CSC1, 
  brand: samsung,
  device: c7proltechn, 
  display: R16NW.C7010ZCS3CSC1, 
  fingerprint: samsung/c7proltezc/c7proltechn:8.0.0/R16NW/C7010ZCS3CSC1:user/release-keys,
    hardware: qcom, 
    host: SWDH2819, 
    id: R16NW, 
    manufacturer: samsung, 
    model: SM-C7010, 
    product: c7proltezc, 
    supported32BitAbis: [armeabi-v7a, armeabi], supported64BitAbis: [arm64-v8a], 
    supportedAbis: [arm64-v8a, armeabi-v7a, armeabi]
    , 
    tags: release-keys, 
    type: user, 
    isPhysicalDevice: true, 
    androidId: 7719a3dbb9dda25a}

你可能感兴趣的:(Flutter,flutter)