flutter截取字符串_在flutter中判断平台,获取设备信息

在生产环境app的开发过程中,获取设备信息是必不可少的一步,本文详细介绍在flutter中如何判断平台是android还是ios,并使用开源库device_info获取android和ios的设备信息。

判断平台

这步很简单,导入平台Platform库就行,位置在dart:io

import 'dart:io';

然后在android studio中编辑的时候就有快捷菜单了:

dd85ef477a7caf93c3a99e3db427ee79.png

判断平台只要使用如下代码:

if(Platform.isIOS){

//ios相关代码

}else if(Platform.isAndroid){

//android相关代码

}

device_info库的使用

先上图

cb0034925d365c54a19ce18f29630bd1.png

7c33d828ec7ffec9bb0ebb7697a1560b.png

1、添加依赖

在pubspec.yml中的dependencies下面新增节点

device_info : ^0.2.0

上图比较快:

4db8defea469cc39f3fa736402b1ca3c.png

然后点下编辑器的"Packages get"或者运行命令行:

flutter packages get

注意这里可能需要配置环境变量:

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

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

2、获取信息

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 supported32BitAbis;

/// An ordered list of 64 bit ABIs supported by this device.

final List supported64BitAbis;

/// An ordered list of ABIs supported by this device.

final List 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真的简单。

如有疑问,请加qq群854192563讨论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值