1.安装protobuf
brew install protobuf
2.安装完成后查看版本,最新版libprotoc 26.1
protoc --version
3.安装dart
brew tap dart-lang/dart
brew install dart
4.dart 安装好后,就有pub命令了。输入dart 命令行和dart pub检查是否成功
5.安装protoc_plugin
dart pub global activate protoc_plugin
6.将protoc-gen-dart所在的pub-cache/bin添加到path变量
vi ~/.zshrc
添加一行
export PATH="$PATH":"$HOME/.pub-cache/bin"
7.将.proto文件编译成dart文件
在~/test/目录创建以下文本文件,保存为user.proto
syntax = "proto3";
message User {
int32 id = 1;
string name = 2;
string email = 3;
enum PhoneType{
MOBILE = 0;
HOME = 1;
WORK = 2;
}
message PhoneNumber{
PhoneType type = 1;
string number = 2;
}
repeated PhoneNumber phoneNumber = 4;
}
cd ~/test/ 目录后,编译为dart可用的文件
#如果没舍之环境变量pub-cache/bin,需要手动指定路径
#protoc --dart_out=. user.proto --plugin ~/.pub-cache/bin/protoc-gen-dart
#第六步已经设置环境变量,则可直接编译
protoc --dart_out=. user.proto
编译出了4个dart文件
我这里生成了4个文件
【语法】
protoc --dart_out=输出目录 proto文本文件
输出目录:.表示当前目录
8.应用,使用Dio解析protobufu
https://www.jianshu.com/p/5952bdc36496
网友评论