在flutter中判斷平臺,獲取設備信息

在生產環境app的開發過程當中,獲取設備信息是必不可少的一步,本文詳細介紹在flutter中如何判斷平臺是android仍是ios,並使用開源庫device_info獲取android和ios的設備信息。android

判斷平臺

這步很簡單,導入平臺Platform庫就行,位置在dart:ioios

import 'dart:io';

而後在android studio中編輯的時候就有快捷菜單了:
平臺判斷快捷菜單app

判斷平臺只要使用以下代碼:編輯器

if(Platform.isIOS){
      //ios相關代碼
    }else if(Platform.isAndroid){
      //android相關代碼
    }

device_info庫的使用

先上圖ide

android

IOS

一、添加依賴

在pubspec.yml中的dependencies下面新增節點ui

device_info : ^0.2.0

上圖比較快:this

依賴

而後點下編輯器的"Packages get"或者運行命令行:spa

flutter packages get

注意這裏可能須要配置環境變量:命令行

export PUB_HOSTED_URL=https://pub.flutter-io.cn
export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn

二、獲取信息

android版本全部屬性

/// Android operating system version values derived from `android.os.Build.VERSION`.
  final AndroidBuildVersion version;

  /// The name of the underlying board, like "goldfish".
  final String board;

  /// The system bootloader version number.
  final String bootloader;

  /// The consumer-visible brand with which the product/hardware will be associated, if any.
  final String brand;

  /// The name of the industrial design.
  final String device;

  /// A build ID string meant for displaying to the user.
  final String display;

  /// A string that uniquely identifies this build.
  final String fingerprint;

  /// The name of the hardware (from the kernel command line or /proc).
  final String hardware;

  /// Hostname.
  final String host;

  /// Either a changelist number, or a label like "M4-rc20".
  final String id;

  /// The manufacturer of the product/hardware.
  final String manufacturer;

  /// The end-user-visible name for the end product.
  final String model;

  /// The name of the overall product.
  final String product;

  /// An ordered list of 32 bit ABIs supported by this device.
  final List<String> supported32BitAbis;

  /// An ordered list of 64 bit ABIs supported by this device.
  final List<String> supported64BitAbis;

  /// An ordered list of ABIs supported by this device.
  final List<String> supportedAbis;

  /// Comma-separated tags describing the build, like "unsigned,debug".
  final String tags;

  /// The type of build, like "user" or "eng".
  final String type;

  /// `false` if the application is running in an emulator, `true` otherwise.
  final bool isPhysicalDevice;

ios版本全部屬性:

/// Device name.
  final String name;

  /// The name of the current operating system.
  final String systemName;

  /// The current operating system version.
  final String systemVersion;

  /// Device model.
  final String model;

  /// Localized name of the device model.
  final String localizedModel;

  /// Unique UUID value identifying the current device.
  final String identifierForVendor;

  /// `false` if the application is running in a simulator, `true` otherwise.
  final bool isPhysicalDevice;

  /// Operating system information derived from `sys/utsname.h`.
  final IosUtsname utsname;

好吧,只要一行依賴就能夠集成兩個平臺的項目代碼,毫無繁瑣配置,flutter真的簡單。debug

若有疑問,請加qq羣854192563討論

相關文章
相關標籤/搜索