1.安装插件
2.在pubspec.yaml添加插件
protobuf: ^2.1.0
protoc_plugin: ^20.0.1
3.安装protoc
brew install protobuf
检查是否安装成功
protoc --version
4.安装dart
brew tap dart-lang/dart
brew install dart
dart 安装好后,就有pub命令了。输入dart 命令行 和dart pub检查是否成功
5.安装protoc_plugin
pub global activate protoc_plugin
6.将protoc-gen-dart添加到path
在用户根目录:.bash_profile添加
export PATH="$PATH":"$HOME/.pub-cache/bin"
安装好后,就有protoc-gen-dart 命令了。(实际上没安装成功,没有生效)
7.将.proto文件编译成dart文件
protoc --dart_out=. test.proto
因为我这里没有成功将protoc-gen-dart添加到path,好在不添加这个环境也可以,在后面添加一段就可以了
protoc --dart_out=. test.proto --plugin ~/.pub-cache/bin/protoc-gen-dart
我这边具体的生成命令行:protoc -I 文件夹绝对路径 --dart_out= 文件夹绝对路径 proto文件绝对路径 --plugin ~/.pub-cache/bin/protoc-gen-dart
protoc -I /Users/macminihao/AndroidStudioProjects/SocialIM/lib/protobufdart/dart/room --dart_out=/Users/macminihao/AndroidStudioProjects/SocialIM/lib/protobufdart/dart/room /Users/macminihao/AndroidStudioProjects/SocialIM/lib/protobufdart/dart/room/room_text.proto --plugin ~/.pub-cache/bin/protoc-gen-dart
最重要的配置环境:
mac相关命令
vim ~/.bash_profile
按 esc 进入命令模式,输入 :wq 退出保存
执行 source ~/.bash_profile 使配置生效
export PUB_HOSTED_URL=https://pub.flutter-io.cn
export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn
FLUTTER_PATH='/Users/macminihao/Library/flutter'
export PATH="$PATH:/Users/macminihao/Library/flutter/bin"
export PATH="$PATH":"$FLUTTER_PATH/bin/cache/dart-sdk/bin"
export PATH="$PATH":"$FLUTTER_PATH/.pub-cache/bin"
export PATH="$PATH":"$HOME/Users/macminihao/Library/flutter/.pub-cache/bin"
//镜像
export PUB_HOSTED_URL=https://pub.flutter-io.cn
export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn
//flutter路径,通用名称,$FLUTTER_PATH指代这个
FLUTTER_PATH='/Users/macminihao/Library/flutter'
//Flutter SDK
export PATH="$PATH:/Users/macminihao/Library/flutter/bin"
//Dart SDK within Flutter SDK
export PATH="$PATH":"$FLUTTER_PATH/bin/cache/dart-sdk/bin"
//Pub cache within Flutter SDK (where the protoc plugin will be located at)
export PATH="$PATH":"$FLUTTER_PATH/.pub-cache/bin"
//这个实际上是protoc-gen-dart,不过不是正确的,没生效
export PATH="$PATH":"$HOME/Users/macminihao/Library/flutter/.pub-cache/bin"