You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从Swift向Flutter传递数据并在Flutter端转换为模型?

解决Flutter端解析iOS归档Uint8List的问题

核心思路:iOS用NSKeyedArchiver归档的是苹果plist格式数据,Flutter需先解析plist,再逐层还原数据。

步骤1:添加依赖

在Flutter项目的pubspec.yaml中添加plist解析依赖:

dependencies:
  plist_parser: ^0.1.0

执行flutter pub get安装依赖。

步骤2:解析顶层归档数据

将Method Channel获取到的Uint8List解析为plist字典:

import 'package:plist_parser/plist_parser.dart';

// 假设从Method Channel拿到的归档数据为archiveData
Map<String, dynamic> topLevelPlist = PlistParser.parseXml(archiveData);

步骤3:解析图片Data数组的子归档

imagesData是单独归档的Data数组,需要二次解析:

// 取出imagesData对应的Uint8List
Uint8List imagesArchive = topLevelPlist['imagesData'] as Uint8List;
// 解析子归档得到图片Data列表
List<Uint8List> imageDataList = PlistParser.parseXml(imagesArchive).values.first as List<Uint8List>;

步骤4:构建Flutter模型

定义你的Flutter数据模型,并初始化:

class ItemModel {
  final String id;
  final List<Uint8List> imageDatas;

  ItemModel({required this.id, required this.imageDatas});
}

// 初始化模型
ItemModel item = ItemModel(
  id: topLevelPlist['id'] as String,
  imageDatas: imageDataList,
);

注意事项

  • 确保iOS端归档imagesData时,使用NSKeyedArchiver.archiveData(withRootObject: 你的Data数组),保证子归档结构正确。
  • 若解析出现类型错误,可先打印topLevelPlist和子归档的结构,确认键名与数据类型匹配。

内容的提问来源于stack exchange,提问作者magic_9527

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 15:00:11