发布时间:2026/7/30 17:48:23
Flutter 获取设备唯一标识 在 Flutter 开发中获取设备的唯一标识是常见需求。在 iOS 和 Android 上由于操作系统的差异获取设备 ID 的方式也有所不同。在本文中我们将讨论如何在 Flutter 中通过 device_info_plus 插件获取 iOS 的设备标识而在 Android 上则通过原生代码获取设备的唯一标识符。效果图1. iOS使用 device_info_plus 插件获取设备唯一标识在 iOS 设备上我们可以通过 device_info_plus 插件轻松获取设备的唯一标识符——identifierForVendor。这是 Apple 提供的设备标识符它对于每个应用都是唯一的。2. Android通过原生代码获取设备唯一标识在 Android 上尽管 device_info_plus 插件能够提供诸多设备信息但无法获取可靠的设备唯一标识。Android 在 Android 10 之后移除了对 Build.SERIAL 的访问权限因此我们需要通过 Android 的 Settings.Secure.ANDROID_ID 结合 Build.SERIAL如果可用来生成一个唯一的设备标识符。我们可以通过 Flutter 的 MethodChannel 调用原生 Android 代码来实现这一功能。Flutter 中的完整代码实现在pubspec.yaml导入device_info_plus: ^8.2.2Dart 代码通过 device_info_plus 和 MethodChannel 获取设备唯一标识import dart:io; import package:device_info_plus/device_info_plus.dart; import package:flutter/material.dart; import package:flutter/services.dart; void main() { runApp(MyApp()); } class MyApp extends StatefulWidget { override _MyAppState createState() _MyAppState(); } class _MyAppState extends StateMyApp { DeviceInfoPlugin deviceInfo DeviceInfoPlugin(); String dvId ; // 存储设备唯一标识 override void initState() { super.initState(); initData(); // 初始化获取设备信息 } override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text(设备唯一标识: $dvId), ], ), ), ), ); } // 初始化设备信息 initData() async { if (Platform.isIOS) { try { // 获取 iOS 设备信息 IosDeviceInfo iosInfo await deviceInfo.iosInfo; dvId iosInfo.identifierForVendor ?? ; // iOS 设备唯一标识 } catch (e) { print(e); } } else if (Platform.isAndroid) { try { // 通过 MethodChannel 获取 Android 唯一标识 const platform MethodChannel(com.example/device_info); String? uniqueId; try { uniqueId await platform.invokeMethod(getUniqueId); } on PlatformException catch (e) { print(e.message); } dvId uniqueId ?? ; } catch (e) { print(e); } } setState(() {}); // 更新 UI } }Android 原生代码通过 MethodChannel 获取设备唯一标识MainActivity.kt设置 MethodChannel 并实现获取 Android 设备唯一标识package com.example.test6 import android.os.Build import android.os.Bundle import io.flutter.embedding.android.FlutterFragmentActivity import io.flutter.embedding.engine.FlutterEngine import io.flutter.plugin.common.MethodChannel class MainActivity : FlutterFragmentActivity() { private val CHANNEL com.example/device_info override fun configureFlutterEngine(flutterEngine: FlutterEngine) { super.configureFlutterEngine(flutterEngine) // 注册 MethodChannel MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result - if (call.method getUniqueId) { // 使用 DeviceUtils 获取设备唯一 ID val deviceUtils DeviceUtils() val uniqueId deviceUtils.getUniqueId(this.applicationContext) if (uniqueId.isNotEmpty()) { result.success(uniqueId) // 返回唯一标识给 Flutter } else { result.error(UNAVAILABLE, Unique ID not available., null) } } else { result.notImplemented() } } } }DeviceUtils.kt生成 Android 设备的唯一标识package com.example.test6 import android.content.Context import android.os.Build import android.provider.Settings import java.security.MessageDigest import java.security.NoSuchAlgorithmException class DeviceUtils { // 获取设备唯一ID fun getUniqueId(context: Context): String { // 获取 Android ID val androidID: String Settings.Secure.getString(context.contentResolver, Settings.Secure.ANDROID_ID) // Build.SERIAL 在 Android 10 已不可用需判断版本号 val serial if (Build.VERSION.SDK_INT Build.VERSION_CODES.Q) { UNKNOWN // 在 Android 10 返回 UNKNOWN } else { Build.SERIAL ?: UNKNOWN } val id androidID serial // 将生成的 ID 转换为 MD5 return try { toMD5(id) } catch (e: NoSuchAlgorithmException) { e.printStackTrace() id } } // 将字符串转换为 MD5 private fun toMD5(text: String): String { val messageDigest: MessageDigest MessageDigest.getInstance(MD5) val digest: ByteArray messageDigest.digest(text.toByteArray()) val sb StringBuilder() for (byte in digest) { val digestInt byte.toInt() and 0xff val hexString Integer.toHexString(digestInt) if (hexString.length 2) { sb.append(0) } sb.append(hexString) } return sb.toString() } }iOS• 我们使用 device_info_plus 插件来获取设备的 identifierForVendor它是 iOS 提供的唯一标识符用于标识设备的应用程序。iOS如果该开发者所有的APP都在该手机上卸载后他获取的id是会变化的所以可以第一次获取到Id后存入到iOS的安全存储里要用的时候拿就行这样就算卸载他也不会清除数据的DeviceInfoPlugin deviceInfo DeviceInfoPlugin(); if (Platform.isIOS) { try { const storage FlutterSecureStorage(); const key device_uuid; IosDeviceInfo iosInfo await deviceInfo.iosInfo; String? udid; try{ udid await storage.read(key: key); if (udid null) { udid await FlutterUdid.udid; await storage.write(key: key, value: udid); } }catch(e){ udid iosInfo.identifierForVendor ?? ; } dvName getIOSDeviceName(iosInfo.utsname.machine); dvModel getIOSDeviceName(iosInfo.utsname.machine); systemVersion iosInfo.systemVersion ?? ; brand iosInfo.utsname.machine ?? ; dvType ios; dvId udid; releaseVersion iosInfo.systemVersion ?? ; } catch (e) { print(e); } }Android• 由于 Android 在 10 版本之后移除了对 Build.SERIAL 的访问权限我们只能通过 Settings.Secure.ANDROID_ID 获取 Android ID然后将其与设备的序列号如果可用拼接起来生成一个唯一标识。• 通过 MethodChannel 机制我们可以在 Flutter 中调用 Android 原生代码。DeviceUtils 类中负责获取唯一标识并将其转换为 MD5 来增加复杂度和唯一性。

相关新闻

2026/7/30 17:43:23

领优惠券app的跳转鉴权与安全防护机制剖析

领优惠券app的跳转鉴权与安全防护机制剖析 大家好,我是省赚客APP研发者微赚淘客! 在返利导购类App中,从用户点击“领券购买”到最终跳转至电商平台完成下单,这一过程是业务的核心链路。然而,这条链路也极易成为黑产攻…

2026/7/30 17:43:23

优惠券app的优惠券分发限流与Redis原子操作实现

优惠券app的优惠券分发限流与Redis原子操作实现 大家好,我是省赚客APP研发者微赚淘客! 在优惠券App的运营活动中,秒杀、限时抢券等高并发场景是家常便饭。当海量用户在同一时刻涌入,试图领取数量有限的优惠券时,系统将…

2026/7/30 17:43:23

优惠券返利app的混合缓存架构与热点Key解决方案

优惠券返利app的混合缓存架构与热点Key解决方案 大家好,我是省赚客APP研发者微赚淘客! 在优惠券返利App中,商品详情、优惠券信息和用户返利数据是访问频率最高的核心数据。面对高并发的用户请求,单一依赖数据库查询无异于自寻死路…

2026/7/30 18:48:29

湖北襄阳有哪些自流平品牌

湖北襄阳有不少自流平品牌,湖北卫筑新材料科技有限公司便是当地颇具代表性的自流平品牌。湖北卫筑新材料科技有限公司概况该公司在自流平领域投入了大量精力进行研发与生产,拥有专业的生产团队和技术人员。公司注重产品质量和创新,致力于为客…

2026/7/30 18:48:29

跑OpenClaw最低要什么配置的迷你主机?从105元到16GB的配置争议背后

围绕OpenClaw的硬件配置讨论,近期出现了两个截然不同的声音。一边是有用户晒出自己用105元的J1800老旧迷你主机跑了两年的OpenClaw,成本极低、运行稳定;另一边是行业普遍认为16GB内存、i5处理器、512GB固态才是“入门硬性门槛”。一台设备的价…

2026/7/30 18:48:29

B站视频下载神器:轻松获取大会员4K高清资源的完整指南

B站视频下载神器:轻松获取大会员4K高清资源的完整指南 【免费下载链接】bilibili-downloader B站视频下载,支持下载大会员清晰度4K,持续更新中 项目地址: https://gitcode.com/gh_mirrors/bil/bilibili-downloader 你是否曾经在B站看到…

2026/7/30 18:48:29

游戏运营和游戏策划的区别?从目标、流程和数据口径对比

游戏运营和游戏策划的区别,核心在于目标导向、工作流程和考核指标完全不同。游戏策划负责从零设计游戏内容、规则和互动体验,而游戏运营则负责产品上线后的用户活跃、留存、付费和内容迭代。一句话概括:策划是“造房子”的人,运营…

2026/7/30 18:43:29

SQLServer 数据库表字段之间的数据迁移:要求不迁移包含中文的remark字段,且迁移完成之后清空对应的remark字段

文章目录 引言(文章快速预览) 需求概述 技术实现 验证方法 I 需求 迁移备注字段到生产任务单号 需求优化 II 实现 查看当前数据库的排序规则 快速验证小测试:“中文”是否被正确拦截了 SELECT 预览 对应的 UPDATE 语句 查看迁移之后的数据 完整sql 引言(文章快速预览) SQ…

2026/7/29 22:32:30

PDF合并与动态水印的工程化方案:2026国内免费工具实测对比

一、背景与测试方案 在实际项目交付中,PDF文件合并与版权保护水印的叠加是一个高频但容易被低估的技术需求。典型的处理链路涉及:多源PDF的文件流合并、页面级水印渲染(含透明度混合与图层叠加)、输出文件体积控制。看似简单的操作…

2026/7/30 0:01:39

[GESP202606 四级] 扫雷

B4557 [GESP202606 四级] 扫雷 https://www.luogu.com.cn/problem/B4557 中国计算机学会(CCF)2026年6月C四级讲解——扫雷 https://www.bilibili.com/video/BV1MCMg6AEXR/ B4557 [GESP202606 四级] 扫雷 https://www.bilibili.com/video/BV1ZKTj6ZEVh/ 2…

2026/7/30 0:01:39

Windows驱动存储终极清理工具:DriverStoreExplorer完全指南

Windows驱动存储终极清理工具:DriverStoreExplorer完全指南 【免费下载链接】DriverStoreExplorer Driver Store Explorer 项目地址: https://gitcode.com/gh_mirrors/dr/DriverStoreExplorer 您是否曾因Windows系统盘空间不足而烦恼?是否遇到过设…

2026/7/29 13:12:43

3个高效策略:快速掌握Axure中文界面配置

3个高效策略:快速掌握Axure中文界面配置 【免费下载链接】axure-cn Chinese language file for Axure RP. Axure RP 简体中文语言包。支持 Axure 11、10、9。不定期更新。 项目地址: https://gitcode.com/gh_mirrors/ax/axure-cn 还在为Axure RP的英文界面感…