網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
本文將引導(dǎo)您完成 2 個(gè)示例,演示如何在 Flutter 中獲取設(shè)備標(biāo)識(shí)符
使用 platform_device_id
如果您只需要運(yùn)行應(yīng)用程序的設(shè)備的 id,最簡(jiǎn)單快捷的解決方案是使用platform_device_id包。它適用于 Android (AndroidId)、iOS (IdentifierForVendor)、Windows (BIOS UUID)、macOS (IOPlatformUUID) 和 Linux (BIOS UUID)。在 Flutter Web 應(yīng)用程序中,您將獲得 UserAgent(此信息不是唯一的)。
應(yīng)用預(yù)覽
我們要構(gòu)建的示例應(yīng)用程序包含一個(gè)浮動(dòng)按鈕。按下此按鈕時(shí),設(shè)備的 ID 將顯示在屏幕上。以下是它在 iOS 和 Android 上的工作方式:
代碼
1.通過(guò)運(yùn)行安裝插件:
flutter pub add platform_device_id
然后執(zhí)行這個(gè)命令:
flutter pub get
不需要特殊權(quán)限或配置。
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 的設(shè)備 ID,并提供有關(guān)設(shè)備的其他詳細(xì)信息(品牌、型號(hào)等)以及 Flutter 應(yīng)用運(yùn)行的 Android 或 iOS 版本。
應(yīng)用預(yù)覽
我們將制作的應(yīng)用程序與上一個(gè)示例中的應(yīng)用程序非常相似。但是,這一次我們將在屏幕上顯示大量文本。返回的結(jié)果因平臺(tái)而異。如您所見,Android 上返回的信息量遠(yuǎn)遠(yuǎn)超過(guò) iOS。
代碼
1. 通過(guò)執(zhí)行以下操作安裝插件:
flutter pub add device_info_plus
然后運(yùn)行:
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), ), ); } }
結(jié)論
我們已經(jīng)介紹了幾種讀取設(shè)備信息的技術(shù)。選擇一個(gè)適合您在項(xiàng)目中實(shí)施的需求。
原文鏈接:https://juejin.cn/post/7084259406421950495
相關(guān)推薦
- 2022-04-17 新版本VS Code 終端設(shè)置為git bash
- 2022-05-25 version `GLIBC_2.18‘ not found
- 2022-06-12 python下grpc與protobuf的編寫使用示例_python
- 2022-06-25 Docker核心組件之聯(lián)合文件系統(tǒng)詳解_docker
- 2023-12-09 使用Redis實(shí)現(xiàn)接口防抖
- 2023-12-25 fiddler展示接口的響應(yīng)時(shí)間
- 2022-12-12 React?高階組件與Render?Props優(yōu)缺點(diǎn)詳解_React
- 2023-01-03 go?doudou開發(fā)gRPC服務(wù)快速上手實(shí)現(xiàn)詳解_Golang
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支