Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate or compile flutter gRPC .proto file in dart

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/src/generated -Iprotos protos/helloworld.proto

this type of error occure

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> 

dependencies used

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 ; 
  }

I also try the following commands

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> 

how to generate or compile the .proto file in dart/flutter

Thanks in advance :)

like image 202
Tushar Nikam Avatar asked Nov 17 '25 10:11

Tushar Nikam


1 Answers

Update

I solve the above problem

note: I am using windows 10 OS with VSCode editor

Steps to solve

1. Install Dart

2 Install the protocol compiler plugin for Dart (protoc-gen-dart) using the following command:

pub global activate protoc_plugin

3 Update your PATH so that the protoc compiler can find the plugin:

  • open environment variables
  • C:\Users[User Name]\AppData\Local\Pub\Cache\bin

4 change directory to lib folder and run the following commands

  • paste your proto folder in lib directory

  • like this flutterProjectName/lib/protos/protoFileName.proto

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

in case if you see this error file missing or file not found then replace this **-Iprotos** name with **--proto_path**

Note: if you see the error like binary something like this, then try the following method

flutter pub cache repair 

or delete Cache folder in this directory C:\Users[User Name]\AppData\Local\Pub\Cache\bin

if all the above things fail delete the cache folder or also check the version updated in the lock file (some time lock give me the problem with updating the version number)

Thank you

like image 156
Tushar Nikam Avatar answered Nov 18 '25 23:11

Tushar Nikam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!