網站首頁 編程語言 正文
本文將引導您完成 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{ 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{ 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), ), ); } }
結論
我們已經介紹了幾種讀取設備信息的技術。選擇一個適合您在項目中實施的需求。
原文鏈接:https://juejin.cn/post/7084259406421950495
相關推薦
- 2022-04-27 前端實現滑動按鈕AJAX與后端交互的示例代碼_AJAX相關
- 2022-06-15 Golang?gin跨域解決方案示例_Golang
- 2022-07-21 react 高價組件HOC實現組件復用
- 2022-05-12 在pycharm中設置快速創建
- 2022-07-30 Linux的磁盤配額設置
- 2021-12-26 WebStorm?發布2021.3重大更新新功能介紹_其它綜合
- 2022-07-26 tensorflow 加載模型時 報錯: ValueError: No variables to s
- 2022-09-05 Linux系統下創建守護進程
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支