【问题标题】:Trying to generate proto to dart file试图生成 proto 到 dart 文件
【发布时间】:2020-01-05 22:55:29
【问题描述】:

我正在尝试使用 protoc-plugin 将 proto 文件生成到 dart 文件,请遵循此指令 https://grpc.io/docs/quickstart/dart/ 但是当我运行此命令行时

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

它显示了这个错误:

'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.

我已经在我的 Android Studio 中安装了 dart 作为插件。是不是我必须安装 dart SDK 并将其添加到环境变量路径才能工作?

更新

在我安装 dart sdk 并重新启动我的电脑后它工作了。

【问题讨论】:

    标签: flutter dart proto


    【解决方案1】:

    看到 Flutter 标签,我认为 Flutter SDK 是您在安装 Dart SDK 之前所拥有的,如更新中所述。

    如果你想这样做而不需要单独安装 Dart SDK(因为 Flutter SDK 本身已经有 Dart SDK),环境必须知道以下位置:

    • 颤振 SDK
      • 例如:export PATH="~/desktop/development/flutter/bin:$PATH"
    • Flutter SDK 中的 Dart SDK
      • 例如:export PATH="~/desktop/development/flutter/bin/cache/dart-sdk/bin:$PATH"
    • Flutter SDK 中的 Pub 缓存(protoc 插件所在的位置)
      • 例如:export PATH="$PATH":"$HOME/desktop/development/flutter/.pub-cache/bin"

    上面给出的示例来自 macOS 中的 .bash_profile,但本质上,在尝试通过 flutter pub global activate protoc_plugin 全局激活插件之前,环境必须知道这 3 个在哪里,然后从 proto 文件生成 Dart 代码。

    【讨论】:

      【解决方案2】:

      文件 protoc-gen-dart.bat 实际上有错误。它试图运行不存在的名为pub 的exe。您需要手动编辑该文件。应该是

      dart pub global run protoc_plugin:protoc_plugin %*

      不是

      pub global run protoc_plugin:protoc_plugin %*

      更改此设置后,我就可以在 Windows 上为 Dart 生成原型

      【讨论】:

        猜你喜欢
        • 2021-03-20
        • 2021-01-27
        • 1970-01-01
        • 2016-12-08
        • 2022-10-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多