flutter 获取设备信息

1、 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;

2、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;

3、 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");

4、项目中获取设备信息

4.1 pubspec.yaml 中添加依赖包
    // device_info: ^0.4.2+4
    device_info_plus: ^3.0.1

注意: Flutter 团队不再对 device_info 插件进行积极维护,推荐 Flutter 社区插件 device_info_plus

4.2 创建 device_info_plus.dart 文件,封装获取设备信息的方法
  import 'dart:io';
  import 'package:device_info_plus/device_info_plus.dart';

    /*
    * @description:  获取设备信息
    * @return {type} 设备信息
    */
    Future<dynamic> getDeviceInfo() async{
        DeviceInfoPlugin deviceInfo = new DeviceInfoPlugin();
        var dataInfo;
        if(Platform.isIOS){
            print('IOS设备:');
            IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
            dataInfo = iosInfo;
        }else if(Platform.isAndroid){
            print('Android设备');
            AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
            dataInfo = androidInfo;
        }
        return dataInfo;
    }

    //  获取设备的指定信息
    class FlutterDeviceInfo{
        // 获取设备的唯一标识 uuid
        static Future<String> get platformUid async {
            var data = await getDeviceInfo(), res;
            if(Platform.isIOS){
                res = data.identifierForVendor;
            }else if(Platform.isAndroid){
                res = data.androidId;
            }
            return res;
        }
        
        //  获取设备name 
        static Future<String> get platformName async {
            var data = await getDeviceInfo(), res;
            if(Platform.isIOS){
                res = data.name;
            }else if(Platform.isAndroid){
                res = data.device;
            }
            return res;
        }

        // 获取设备的model
        static Future<String> get platformModel async {
            var data = await getDeviceInfo(), res;
            if (Platform.isIOS) {
                res = data.utsname.machine;
            } else if (Platform.isAndroid) {
                res = data.brand + ' ' + data.model;
            }
            return res;
       }
    }
4.3 项目组件中使用
    // 引入依赖包
    import '../utils/device_info_plus.dart';

    // 组件中使用
    RaisedButton(
        child: Text("获取设备信息"),
        onPressed: ()async{
            var platformUid = await FlutterDeviceInfo.platformUid;
            var platformName = await FlutterDeviceInfo.platformName;
            var platformModel = await FlutterDeviceInfo.platformModel;
            print( ' platformUid: $platformUid , platformName: $platformName , platformModel: $platformModel');
            // 模拟器输出结果:  platformUid: RPB2.200611.009 , platformName: generic_x86_64_arm64 , platformModel: sdk_gphone_x86_64
        }
    )
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容