经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Flutter » 查看文章
详解如何在Flutter中获取设备标识符
来源:jb51  时间:2022/4/11 12:27:24  对本文有异议

本文将引导您完成 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.通过运行安装插件:

  1. flutter pub add platform_device_id

然后执行这个命令:

  1. flutter pub get

不需要特殊权限或配置。

2.完整代码:

  1. // main.dart
  2. import 'package:flutter/material.dart';
  3. import 'package:platform_device_id/platform_device_id.dart';
  4. ?
  5. void main() {
  6. runApp(const MyApp());
  7. }
  8. ?
  9. class MyApp extends StatelessWidget {
  10. const MyApp({Key? key}) : super(key: key);
  11. @override
  12. Widget build(BuildContext context) {
  13. return MaterialApp(
  14. // Remove the debug banner
  15. debugShowCheckedModeBanner: false,
  16. title: '大前端之旅',
  17. theme: ThemeData(
  18. primarySwatch: Colors.indigo,
  19. ),
  20. home: const HomePage());
  21. }
  22. }
  23. ?
  24. class HomePage extends StatefulWidget {
  25. const HomePage({Key? key}) : super(key: key);
  26. ?
  27. @override
  28. _HomePageState createState() => _HomePageState();
  29. }
  30. ?
  31. class _HomePageState extends State<HomePage> {
  32. String? _id;
  33. ?
  34. // This function will be called when the floating button is pressed
  35. void _getInfo() async {
  36. // Get device id
  37. String? result = await PlatformDeviceId.getDeviceId;
  38. ?
  39. // Update the UI
  40. setState(() {
  41. _id = result;
  42. });
  43. }
  44. ?
  45. @override
  46. Widget build(BuildContext context) {
  47. return Scaffold(
  48. appBar: AppBar(title: const Text('大前端之旅')),
  49. body: Padding(
  50. padding: const EdgeInsets.all(20),
  51. child: Center(
  52. child: Text(
  53. _id ?? 'Press the button',
  54. style: TextStyle(fontSize: 20, color: Colors.red.shade900),
  55. )),
  56. ),
  57. floatingActionButton: FloatingActionButton(
  58. onPressed: _getInfo, child: const Icon(Icons.play_arrow)),
  59. );
  60. }
  61. }

使用 device_info_plus

device_info_plus为您提供作为 platform_device_id 的设备 ID,并提供有关设备的其他详细信息(品牌、型号等)以及 Flutter 应用运行的 Android 或 iOS 版本。

应用预览

我们将制作的应用程序与上一个示例中的应用程序非常相似。但是,这一次我们将在屏幕上显示大量文本。返回的结果因平台而异。如您所见,Android 上返回的信息量远远超过 iOS。

代码

1. 通过执行以下操作安装插件:

  1. flutter pub add device_info_plus

然后运行:

  1. flutter pub get

2. main.dart中的完整源代码:

  1. // main.dart
  2. import 'package:flutter/material.dart';
  3. import 'package:device_info_plus/device_info_plus.dart';
  4. ?
  5. void main() {
  6. runApp(const MyApp());
  7. }
  8. ?
  9. class MyApp extends StatelessWidget {
  10. const MyApp({Key? key}) : super(key: key);
  11. @override
  12. Widget build(BuildContext context) {
  13. return MaterialApp(
  14. // Remove the debug banner
  15. debugShowCheckedModeBanner: false,
  16. title: '大前端之旅',
  17. theme: ThemeData(
  18. primarySwatch: Colors.amber,
  19. ),
  20. home: const HomePage());
  21. }
  22. }
  23. ?
  24. class HomePage extends StatefulWidget {
  25. const HomePage({Key? key}) : super(key: key);
  26. ?
  27. @override
  28. _HomePageState createState() => _HomePageState();
  29. }
  30. ?
  31. class _HomePageState extends State<HomePage> {
  32. Map? _info;
  33. ?
  34. // This function is triggered when the floating button gets pressed
  35. void _getInfo() async {
  36. // Instantiating the plugin
  37. final deviceInfoPlugin = DeviceInfoPlugin();
  38. ?
  39. final result = await deviceInfoPlugin.deviceInfo;
  40. setState(() {
  41. _info = result.toMap();
  42. });
  43. }
  44. ?
  45. @override
  46. Widget build(BuildContext context) {
  47. return Scaffold(
  48. appBar: AppBar(title: const Text('大前端之旅')),
  49. body: _info != null
  50. ? Padding(
  51. padding: const EdgeInsets.all(20),
  52. child: ListView(
  53. children: _info!.entries
  54. .map((e) => Wrap(
  55. children: [
  56. Text(
  57. "${e.key} :",
  58. style: const TextStyle(
  59. fontSize: 18, color: Colors.red),
  60. ),
  61. const SizedBox(
  62. width: 15,
  63. ),
  64. Text(
  65. e.value.toString(),
  66. style: const TextStyle(
  67. fontSize: 18,
  68. ),
  69. )
  70. ],
  71. ))
  72. .toList(),
  73. ),
  74. )
  75. : const Center(
  76. child: Text('Press the button'),
  77. ),
  78. floatingActionButton: FloatingActionButton(
  79. onPressed: _getInfo,
  80. child: const Icon(Icons.info),
  81. ),
  82. );
  83. }
  84. }

结论

我们已经介绍了几种读取设备信息的技术。选择一个适合您在项目中实施的需求。

以上就是详解如何在Flutter中获取设备标识符的详细内容,更多关于Flutter获取设备标识符的资料请关注w3xue其它相关文章!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号