本文将引导您完成 2 个示例,演示如何在 flutter 中获取设备标识符
使用 platform_device_id
如果您只需要运行应用程序的设备的 id,最简单快捷的解决方案是使用platform_device_id包。它适用于 android (androidid)、ios (identifierforvendor)、windows (bios uuid)、macos (ioplatformuuid) 和 linux (bios uuid)。在 flutter web 应用程序中,您将获得 useragent(此信息不是唯一的)。
应用预览
我们要构建的示例应用程序包含一个浮动按钮。按下此按钮时,设备的 id 将显示在屏幕上。以下是它在 ios 和 android 上的工作方式:
代码
1.通过运行安装插件:
flutter pub add platform_device_id
然后执行这个命令:
flutter pub get
不需要特殊权限或配置。
2.完整代码:
// main.dart import 'package:flutter/material.dart'; import 'package:platform_device_id/platform_device_id.dart'; void main() { runapp(const myapp()); } class myapp extends statelesswidget { const myapp({key? key}) : super(key: key); @override widget build(buildcontext context) { return materialapp( // remove the debug banner debugshowcheckedmodebanner: false, title: '大前端之旅', theme: themedata( primaryswatch: colors.indigo, ), home: const homepage()); } } class homepage extends statefulwidget { const homepage({key? key}) : super(key: key); @override _homepagestate createstate() => _homepagestate(); } class _homepagestate extends state<homepage> { string? _id; // this function will be called when the floating button is pressed void _getinfo() async { // get device id string? result = await platformdeviceid.getdeviceid; // update the ui setstate(() { _id = result; }); } @override widget build(buildcontext context) { return scaffold( appbar: appbar(title: const text('大前端之旅')), body: padding( padding: const edgeinsets.all(20), child: center( child: text( _id ?? 'press the button', style: textstyle(fontsize: 20, color: colors.red.shade900), )), ), floatingactionbutton: floatingactionbutton( onpressed: _getinfo, child: const icon(icons.play_arrow)), ); } }
使用 device_info_plus
包device_info_plus为您提供作为 platform_device_id 的设备 id,并提供有关设备的其他详细信息(品牌、型号等)以及 flutter 应用运行的 android 或 ios 版本。
应用预览
我们将制作的应用程序与上一个示例中的应用程序非常相似。但是,这一次我们将在屏幕上显示大量文本。返回的结果因平台而异。如您所见,android 上返回的信息量远远超过 ios。
代码
1. 通过执行以下操作安装插件:
flutter pub add device_info_plus
然后运行:
flutter pub get
2. main.dart中的完整源代码:
// main.dart import 'package:flutter/material.dart'; import 'package:device_info_plus/device_info_plus.dart'; void main() { runapp(const myapp()); } class myapp extends statelesswidget { const myapp({key? key}) : super(key: key); @override widget build(buildcontext context) { return materialapp( // remove the debug banner debugshowcheckedmodebanner: false, title: '大前端之旅', theme: themedata( primaryswatch: colors.amber, ), home: const homepage()); } } class homepage extends statefulwidget { const homepage({key? key}) : super(key: key); @override _homepagestate createstate() => _homepagestate(); } class _homepagestate extends state<homepage> { map? _info; // this function is triggered when the floating button gets pressed void _getinfo() async { // instantiating the plugin final deviceinfoplugin = deviceinfoplugin(); final result = await deviceinfoplugin.deviceinfo; setstate(() { _info = result.tomap(); }); } @override widget build(buildcontext context) { return scaffold( appbar: appbar(title: const text('大前端之旅')), body: _info != null ? padding( padding: const edgeinsets.all(20), child: listview( children: _info!.entries .map((e) => wrap( children: [ text( "${e.key} :", style: const textstyle( fontsize: 18, color: colors.red), ), const sizedbox( width: 15, ), text( e.value.tostring(), style: const textstyle( fontsize: 18, ), ) ], )) .tolist(), ), ) : const center( child: text('press the button'), ), floatingactionbutton: floatingactionbutton( onpressed: _getinfo, child: const icon(icons.info), ), ); } }
结论
我们已经介绍了几种读取设备信息的技术。选择一个适合您在项目中实施的需求。
以上就是详解如何在flutter中获取设备标识符的详细内容,更多关于flutter获取设备标识符的资料请关注其它相关文章!