hoangquyy
hoangquyy

Reputation: 2073

Trying to generate proto to dart file

I'm trying to generated proto file to dart file with protoc-plugin follow this instruction https://grpc.io/docs/quickstart/dart/ but when I run this command line

protoc --dart_out=grpc:lib/proto --plugin=protoc-gen-dart=C:/src/flutter/flutter/.pub-cache/bin/protoc-gen-dart.bat -Iprotobuf protobuf/utils.proto

And it showed this error:

'dart' is not recognized as an internal or external command,
operable program or batch file.
'pub' is not recognized as an internal or external command,
operable program or batch file.
--dart_out: protoc-gen-dart: Plugin failed with status code 1.

I have installed dart as a plugin in my Android Studio. Is that I have to install dart SDK and add it to environment variable path to work?

Update

It worked after i install dart sdk and restart my pc.

Upvotes: 5

Views: 4506

Answers (2)

Christian Findlay
Christian Findlay

Reputation: 7692

The file protoc-gen-dart.bat actually has bugs in it. It's trying to run exe called pub which doesn't exist. You need to manually edit the file. It should be

dart pub global run protoc_plugin:protoc_plugin %*

not

pub global run protoc_plugin:protoc_plugin %*

Once I changed this, I was able to generate protos for Dart on Windows

Upvotes: 1

benChung
benChung

Reputation: 406

Seeing the Flutter tag, I presume that Flutter SDK was what you had before you install Dart SDK as said in your update.

If you want to do this without having to install Dart SDK separately (since Flutter SDK has Dart SDK within itself already), the environment has to know where the following are at:

  • Flutter SDK
    • eg: export PATH="~/desktop/development/flutter/bin:$PATH"
  • Dart SDK within Flutter SDK
    • eg: export PATH="~/desktop/development/flutter/bin/cache/dart-sdk/bin:$PATH"
  • Pub cache within Flutter SDK (where the protoc plugin will be located at)
    • eg: export PATH="$PATH":"$HOME/desktop/development/flutter/.pub-cache/bin"

The given examples above are from .bash_profile in macOS, but in essence, the environment has to know where these 3 are before trying to globally activate the plugin via flutter pub global activate protoc_plugin and then generate Dart code from proto files.

Upvotes: 4

Related Questions