美文网首页
mac安装proto以及简单编译为dart文件

mac安装proto以及简单编译为dart文件

作者: 晓函 | 来源:发表于2024-04-04 13:43 被阅读0次

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

相关文章

网友评论

      本文标题:mac安装proto以及简单编译为dart文件

      本文链接:https://www.haomeiwen.com/subject/vgentjtx.html