proto文件编译部分:
-
Dart plugin for the protocol compiler:
-
Install the protocol compiler plugin for Dart (
protoc-gen-dart
) using the following command:安装针对Dart的protocol编译插件
$ dart pub global activate protoc_plugin
-
Update your
PATH
so that theprotoc
compiler can find the plugin:添加到全局变量中
$ export PATH="$PATH:$HOME/.pub-cache/bin"
-
From the example/helloworld
directory, run the following command:
编译一个proto文件
$ protoc --dart_out=grpc:lib/src/generated -Iprotos protos/helloworld.proto
注意:
编译时,必须保证lib/src/generated 这个路径的文件目录是存在的,proto文件必须放到protos目录下
flutter 使用gRPC部分:
Run this command:
With Dart:
$ dart pub add grpc
With Flutter:
$ flutter pub add grpc
This will add a line like this to your package’s pubspec.yaml (and run an implicit dart pub get
):
dependencies:
grpc: ^3.1.0
Alternatively, your editor might support dart pub get
or flutter pub get
. Check the docs for your editor to learn more.
Import it
Now in your Dart code, you can use:
import 'package:grpc/grpc.dart';
grpc-dart在flutter使用存在的问题