美文网首页Flutter相关Flutter
Flutter如何获取设备信息

Flutter如何获取设备信息

作者: 只能陪你走一程 | 来源:发表于2019-05-10 16:53 被阅读0次

    一.前言

    App开发中,在调用后台接口时可能需要通过设备的信息来生成鉴权,或者设备的异常登录检测,这个时候就需要获取设备的信息,那么我们如何在Flutter应用程序获取设备信息呢?.

    二.获取平台信息

    在前两天Google的IO大会上,已经公开Flutter时全平台框架,可以在多种平台上运行,如:Linux,mac,android,windows,ios,获取设备信息前首先要确定我们的程序运行在哪一种平台之上,判断平台我们可以通过,dart.io包下的Platform类来获取.

    Platform类下的代码:

      /**
       * Whether the operating system is a version of
       * [Linux](https://en.wikipedia.org/wiki/Linux).
       *
       * This value is `false` if the operating system is a specialized
       * version of Linux that identifies itself by a different name,
       * for example Android (see [isAndroid]).
       */
      static final bool isLinux = (_operatingSystem == "linux");
    
      /**
       * Whether the operating system is a version of
       * [macOS](https://en.wikipedia.org/wiki/MacOS).
       */
      static final bool isMacOS = (_operatingSystem == "macos");
    
      /**
       * Whether the operating system is a version of
       * [Microsoft Windows](https://en.wikipedia.org/wiki/Microsoft_Windows).
       */
      static final bool isWindows = (_operatingSystem == "windows");
    
      /**
       * Whether the operating system is a version of
       * [Android](https://en.wikipedia.org/wiki/Android_%28operating_system%29).
       */
      static final bool isAndroid = (_operatingSystem == "android");
    
      /**
       * Whether the operating system is a version of
       * [iOS](https://en.wikipedia.org/wiki/IOS).
       */
      static final bool isIOS = (_operatingSystem == "ios");
    
      /**
       * Whether the operating system is a version of
       * [Fuchsia](https://en.wikipedia.org/wiki/Google_Fuchsia).
       */
      static final bool isFuchsia = (_operatingSystem == "fuchsia");
    

    使用

    1.导入io包

    import 'dart:io';
    

    2.使用

    if(Platform.isIOS){
      //ios相关代码
      print('IOS设备:');
    }else if(Platform.isAndroid)
      //android相关代码
      print('Android设备');
    }
    
    三.获取设备信息

    判断平台完平台之后,我们接着来获取设备信息,获取设备信息,我们通过依赖第三方的库device_info

    使用

    1项目的.pubspec.yaml文件中添加依赖

    dependencies:
      device_info : ^0.2.0
    

    2.安装下载依赖包

    flutter packages get
    

    3.导入device_info.dart:

    import 'package:device_info/device_info.dart';
    

    4.获取Android和IOS的设备信息

      void getDeviceInfo() async{
        DeviceInfoPlugin deviceInfo = new DeviceInfoPlugin();
        if(Platform.isIOS){
          print('IOS设备:');
          IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
        }else if(Platform.isAndroid){
          print('Android设备');
          AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
        }
      }
    

    IOS设备属性(取自IosDeviceInfo 类)

    class IosDeviceInfo {
      IosDeviceInfo._({
        this.name,
        this.systemName,
        this.systemVersion,
        this.model,
        this.localizedModel,
        this.identifierForVendor,
        this.isPhysicalDevice,
        this.utsname,
      });
    }
    

    Android设备属性(取自AndroidDeviceInfo类)

    class AndroidDeviceInfo {
      AndroidDeviceInfo._({
        this.version,
        this.board,
        this.bootloader,
        this.brand,
        this.device,
        this.display,
        this.fingerprint,
        this.hardware,
        this.host,
        this.id,
        this.manufacturer,
        this.model,
        this.product,
        List<String> supported32BitAbis,
        List<String> supported64BitAbis,
        List<String> supportedAbis,
        this.tags,
        this.type,
        this.isPhysicalDevice,
      })  : supported32BitAbis = new List<String>.unmodifiable(supported32BitAbis),
            supported64BitAbis = new List<String>.unmodifiable(supported64BitAbis),
            supportedAbis = new List<String>.unmodifiable(supportedAbis);
    
      /// 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;
    }
    

    四.总结

    关于Flutter获取设备信息的文章,在各个平台上都有类似的文章,也是通过学习前辈们的知识,发布出来仅仅作为笔记,如有错误之处请指点.

    相关文章

      网友评论

        本文标题:Flutter如何获取设备信息

        本文链接:https://www.haomeiwen.com/subject/avvaoqtx.html