Flutter 鸿蒙化实战:mobile_device_identifier 适配 OpenHarmony

前言

随着鸿蒙生态的快速发展,越来越多的 Flutter 应用需要适配 OpenHarmony 平台。但生态早期,大量常用三方库只有 Android / iOS 实现,鸿蒙侧只能自己造轮子。

为了解决这个问题,CPF-Flutter 社区对一批高频使用的 Flutter 三方库做了 OpenHarmony 平台适配,统一托管在 GitCode 的 CPF-Flutter 组织 下,全部通过 TAG 版本隔离、配套门禁编译验证,开箱即用。

本篇是系列文章之一,主角是 mobile_device_identifier —— 设备唯一标识插件。

mobile_device_identifier 是什么

mobile_device_identifier(原库 0.0.3)用于获取移动设备的唯一标识:

  1. 设备 IDgetDeviceId 获取设备唯一标识(iOS 的 identifierForVendor / Android 的 AndroidId 语义)。

适配情况

Flutter 框架版本TAG 名称分支名
3.70.0.3-ohos-1.0.0br_v0.0.3_ohos
3.220.0.3-ohos-1.0.0br_v0.0.3_ohos
3.270.0.3-ohos-1.0.0br_v0.0.3_ohos
3.350.0.3-ohos-1.0.0br_v0.0.3_ohos

快速接入

在 pubspec.yaml 中添加 git 依赖:

dependencies:
  mobile_device_identifier:
    git:
      url: https://gitcode.com/CPF-Flutter/fluttertpc_mobile_device_identifier.git
      # 按你的 Flutter 框架版本,从上方映射表选择对应 TAG
      ref: 0.0.3-ohos-1.0.0

执行:

flutter pub get

使用示例

import 'package:mobile_device_identifier/mobile_device_identifier.dart';

// 获取设备唯一标识
final deviceId = await MobileDeviceIdentifier().getDeviceId();
print('设备 ID: $deviceId');

API 支持一览

API说明
getDeviceId获取设备唯一标识

[TIP] 本库鸿蒙版与上游 API 对齐,接入方式与 iOS / Android 一致。

仓库地址

  • OpenHarmony 适配仓库:https://gitcode.com/CPF-Flutter/fluttertpc_mobile_device_identifier
  • 使用问题反馈:请到 CPF-Flutter 社区 Issues 提交问题(社区统一收集跟进);本仓库 Issues 区亦可。

系列预告

截至目前,CPF-Flutter 社区已完成 250+ 个 Flutter 三方库的 OpenHarmony 平台适配,覆盖权限管理、UI 组件、设备信息、位置服务、媒体处理、网络、支付等多个领域,完整的适配清单(含每个库的版本映射表)见:

  • 适配总览文档:https://gitcode.com/CPF-Flutter/docs/blob/main/ThirdpartyLibrarites.md
  • 仓库组织:https://gitcode.com/CPF-Flutter

本系列后续会陆续挑选高频库展开讲解,比如资源定位(finder)、平台适配(flutter_adapter)、屏幕信息(screen_retriever)等。

如果你的 Flutter 应用正在计划适配鸿蒙,欢迎关注 CPF-Flutter 组织,也欢迎参与共建。

Logo

作为“人工智能6S店”的官方数字引擎,为AI开发者与企业提供一个覆盖软硬件全栈、一站式门户。

更多推荐