詳解如何在Flutter中獲取設備標識符
本文將引導您完成 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
然后執(zhí)行這個命令:
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. 通過執(zhí)行以下操作安裝插件:
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), ), ); } }
結論
我們已經(jīng)介紹了幾種讀取設備信息的技術。選擇一個適合您在項目中實施的需求。
以上就是詳解如何在Flutter中獲取設備標識符的詳細內容,更多關于Flutter獲取設備標識符的資料請關注腳本之家其它相關文章!
相關文章
Android 兩個ViewPager的聯(lián)動效果的實現(xiàn)
這篇文章主要介紹了Android 兩個ViewPager的聯(lián)動效果的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08Core Animation一些Demo總結 (動態(tài)切換圖片、大轉盤、圖片折疊、進度條等動畫效果)
這篇文章主要介紹了Core Animation一些Demo總結 (動態(tài)切換圖片、大轉盤、圖片折疊、進度條等動畫效果)的相關資料,需要的朋友可以參考下2016-02-02Android 利用ViewPager+GridView實現(xiàn)首頁導航欄布局分頁效果
用ViewPager+GridView實現(xiàn)首頁導航欄布局分頁效果來實現(xiàn)的效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2016-10-10Android用戶輸入自動提示控件AutoCompleteTextView使用方法
這篇文章主要為大家詳細介紹了Android用戶輸入自動提示控件AutoCompleteTextView的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08