【问题标题】:How to generate or compile flutter gRPC .proto file in dart如何在 dart 中生成或编译 Flutter gRPC .proto 文件
【发布时间】:2021-03-20 21:12:24
【问题描述】:

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

$ protoc --dart_out=grpc:lib/src/generated -Iprotos protos/helloworld.proto

发生这种类型的错误

PS G:\Flutter Implelemt\helloworld> protoc --dart_out=grpc:lib/src/generated -Iprotos 
protos/helloworld.proto
protos: warning: directory does not exist.
Could not make proto path relative: protos/helloworld.proto: No such file or directory
PS G:\Flutter Implelemt\helloworld> 

使用的依赖项

grpc: ^0.6.6
protobuf: ^1.1.0
protoc_plugin: ^19.2.0+1

helloworld.proto

syntax = "proto3";
package helloworld;

service  Greeter  { 
       rpc  SayHello  ( HelloRequest )  returns  ( HelloReply )  {} 
  }

  message  HelloRequest  { 
      string  name  =  1 ; 
    }

  message  HelloReply  { 
       string  message  =  1 ; 
  }

我也试试下面的命令

PS G:\Flutter Implelemt\helloworld> protoc --dart_out=grpc:lib/src/pb --proto_path 
lib/protos/helloworld.proto  
Missing input file.
PS G:\Flutter Implelemt\helloworld> 

如何在dart/flutter中生成或编译.proto文件

在此先感谢 :)

【问题讨论】:

  • 您好,您的问题解决了吗?

标签: flutter dart grpc dart-pub proto


【解决方案1】:

确保您的输出目录存在,并且您的输入文件相对路径正确。查看您的错误消息:

protos: warning: directory does not exist.
Could not make proto path relative: protos/helloworld.proto: No such file or directory

我们可以知道,protos/helloworld.proto 似乎不存在。

【讨论】:

    【解决方案2】:

    更新

    我解决了上面的问题

    注意:我正在使用带有 VSCode 编辑器的 Windows 10 操作系统

    解决步骤

    1. 安装 Dart

    2 使用以下命令为 Dart (protoc-gen-dart) 安装协议编译器插件:

    pub global activate protoc_plugin
    

    3 更新您的 PATH 以便 protoc 编译器可以找到插件:

    • 打开环境变量
    • C:\Users[用户名]\AppData\Local\Pub\Cache\bin

    4将目录切换到lib文件夹并运行以下命令

    • 将你的 proto 文件夹粘贴到 lib 目录中

    • 像这样 flutterProjectName/lib/protos/protoFileName.proto

       protoc --dart_out=grpc:src/generated -Iprotos protos/helloworld.proto
      

    如果您看到此错误file missing or file not found then replace this **-Iprotos** name with **--proto_path**

    注意:如果你看到类似二进制这样的错误,那么试试下面的方法

    flutter pub cache repair 
    

    或删除此目录下的Cache文件夹C:\Users[用户名]\AppData\Local\Pub\Cache\bin

    如果以上所有操作都失败,请删除缓存文件夹或检查锁定文件中更新的版本(有时锁定会给我更新版本号的问题)

    谢谢

    【讨论】:

      猜你喜欢
      • 2020-04-09
      • 2018-01-07
      • 2020-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-05
      • 1970-01-01
      • 2021-01-27
      相关资源
      最近更新 更多