Flutter序列化与反序列化插件proto_bytes的使用

Flutter proto_bytes 库的使用

pub

proto_bytes 是一个用于二进制通信协议序列化与反序列化的底层依赖库。该库非常轻量级,不包含任何网络功能,仅专注于数据对象的序列化与反序列化。

关于 ProtoBytes 的更多详细信息,可以查看其 GitHub 仓库


使用

通过使用 ProtoBytes 创建的协议模板编译生成的 ExampleInfo 对象为例:

ExampleInfo 的序列化

ExampleInfo info = ExampleInfo();
info.byteProp = 1;
info.shortProp = 2;
info.doubleProp = pi; // 使用 Dart 的数学常量 π
info.longstringProp = 'hello word'; // 设置长字符串属性
info.intListProp.addAll([1, 2, 3, 4, 5]); // 添加整数列表

// 创建 AttInfo 对象并添加到列表
AttInfo attInfo = AttInfo();
attInfo.attId = 1;
info.attInfo1ListProp.add(attInfo);

attInfo = AttInfo();
attInfo.attId = 2;
info.attInfo1ListProp.add(attInfo);

// 将 ExampleInfo 对象序列化为字节数组
List<int> bytes = ExampleInfo.toBytes(info);

ExampleInfo 的反序列化

List<int> bytes = ...; // 假设已经获取序列化后的字节数组
ExampleInfo info2 = ExampleInfo.fromBytes(bytes); // 反序列化为 ExampleInfo 对象
print(info2); // 输出反序列化后的对象信息

完整示例代码

以下是一个完整的示例代码,展示了如何使用 proto_bytes 进行序列化与反序列化。

示例代码

import 'dart:math';

import 'package:example/com/myprotos/infos/att_info.dart';
import 'package:example/com/myprotos/infos/example_info.dart';
import 'package:proto_bytes/com/protobytes/utils/byte_array.dart';

void main() {
  // 创建 ExampleInfo 实例并设置属性
  ExampleInfo info = ExampleInfo();
  info.byteProp = 1;
  info.shortProp = 2;
  info.doubleProp = pi; // 使用 Dart 的数学常量 π
  info.longstringProp = 'hello word'; // 设置长字符串属性
  info.intListProp.addAll([1, 2, 3, 4, 5]); // 添加整数列表

  // 创建 AttInfo 对象并添加到 ExampleInfo 的列表中
  AttInfo attInfo = AttInfo();
  attInfo.attId = 1;
  info.attInfo1ListProp.add(attInfo);

  attInfo = AttInfo();
  attInfo.attId = 2;
  info.attInfo1ListProp.add(attInfo);

  // 将 ExampleInfo 对象序列化为字节数组
  List<int> bytes = ExampleInfo.toBytes(info);
  print('序列化后的字节数组:');
  print(bytes);

  // 将字节数组反序列化为 ExampleInfo 对象
  ExampleInfo info2 = ExampleInfo.fromBytes(bytes);
  print('反序列化后的对象:');
  print(info2);
}

输出结果

运行上述代码后,您将看到类似以下的输出:

序列化后的字节数组:
[1, 2, 64, 23, 111, 108, 108, 101, 104, 32, 119, 111, 114, 108, 100, 0, 0, 0, 0, 0, 0, 0, 167, 140, 193, 127, 1, 2, 3, 4, 5, 1, 2]
反序列化后的对象:
ExampleInfo(byteProp: 1, shortProp: 2, doubleProp: 3.141592653589793, longstringProp: hello word, intListProp: [1, 2, 3, 4, 5], attInfo1ListProp: [AttInfo(attId: 1), AttInfo(attId: 2)])

更多关于Flutter序列化与反序列化插件proto_bytes的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter序列化与反序列化插件proto_bytes的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


在Flutter中,序列化和反序列化是常见的操作,特别是在与后端通信或存储数据时。proto_bytes 是一个用于处理 Protocol Buffers(protobuf)的插件,它可以帮助你将 Dart 对象序列化为二进制格式,或者将二进制数据反序列化为 Dart 对象。

以下是如何在 Flutter 中使用 proto_bytes 插件的基本步骤:

1. 添加依赖

首先,你需要在 pubspec.yaml 文件中添加 protobufproto_bytes 的依赖:

dependencies:
  flutter:
    sdk: flutter
  protobuf: ^2.0.0
  proto_bytes: ^1.0.0

然后运行 flutter pub get 来获取依赖。

2. 定义 Protobuf 消息

你需要先定义你的 Protobuf 消息。假设你有一个简单的 .proto 文件如下:

syntax = "proto3";

message Person {
  string name = 1;
  int32 age = 2;
}

使用 protoc 编译器来生成 Dart 代码。你可以使用 protoc_plugin 插件来生成 Dart 文件:

protoc --dart_out=. person.proto

这会生成一个 person.pb.dart 文件,其中包含 Person 类的定义。

3. 使用 proto_bytes 进行序列化和反序列化

序列化

将 Dart 对象序列化为二进制数据:

import 'person.pb.dart';
import 'package:proto_bytes/proto_bytes.dart';

void main() {
  var person = Person()
    ..name = 'John Doe'
    ..age = 30;

  // 序列化为二进制数据
  List<int> bytes = protoBytes.encode(person);

  print('Serialized bytes: $bytes');
}

反序列化

将二进制数据反序列化为 Dart 对象:

import 'person.pb.dart';
import 'package:proto_bytes/proto_bytes.dart';

void main() {
  // 假设这是你收到的二进制数据
  List<int> bytes = [10, 8, 74, 111, 104, 110, 32, 68, 111, 101, 16, 30];

  // 反序列化为 Dart 对象
  Person person = protoBytes.decode<Person>(bytes);

  print('Deserialized person: ${person.name}, ${person.age}');
}

4. 处理复杂场景

如果你有更复杂的 Protobuf 消息结构,proto_bytes 也可以处理嵌套消息、枚举等。只需确保你的 .proto 文件定义正确,并生成相应的 Dart 代码。

5. 错误处理

在实际使用中,序列化和反序列化可能会遇到错误(例如,二进制数据格式不正确)。你可以使用 try-catch 来捕获和处理这些错误:

try {
  Person person = protoBytes.decode<Person>(bytes);
  print('Deserialized person: ${person.name}, ${person.age}');
} catch (e) {
  print('Failed to deserialize: $e');
}
回到顶部