Flutter with gRPC made easy :)
It has been a while now for me exploring flutter, such that it suits best to Nuclei’s mobile development requirements and technology stack. Having said that, one of the most important aspect was to figure out dart’s support for gRPC and protocol buffers. But finally, after pretty much efforts I have been able to integrate gRPC with flutter on top of a service in goLang.Thanks to the very sporty and widely growing Flutter community. Cheers!The roadblocks and challenges that I came across, have driven me to assemble all the bits and pieces at one place and write this article, such that it helps all the beginners who want an easy way out through Flutter with gRPC.Key requirements:1. __A simple service__ — (I have chosen goLang to host the service, you can choose any other language according to your requirement)2. __Flutter client__ — (I have written the client in android studio, you can go for VS code if you want)3. __Basic understanding__ of gRPC, protocol buffers and understanding of programming in goLang and dart# ServerLet’s begin with setting up gRPC for our GO server1. Download protoc compiler https://github.com/protocolbuffers/protobuf/releases2. Unzip the package and add executable protoc under bin to PATH -> /usr/local/bin3. Install go on your system4. Type the following on your terminal to get go specific gRPC package and package for proper support with protocol buffers```bashgo get -u google.golang.org/grpcgo get -u github.com/golang/protobuf/protoc-gen-go```Lets create a simple proto file named as server.protoAs it is quite evident from the proto that the service takes two numbers a and b as a Request and returns the sum of both as result in form of Response.!![Fig 1 : server.proto](//images.ctfassets.net/urdv1oztwvyo/5g7pvV4o3S9AErQAOFUCUD/b8a33073b30bbdb07933f5f3f93421cf/1_9sBqPb4XAzR-HK0Pw1XkTQ__1_.png)In order to generate a corresponding go file for our proto file, we need access to all include files that comes when we download the protoc compiler.Locate the unzipped protoc directory in your system, and locate the directory include with the absolute path protoc/include. Refer: Fig 2Copy the whole directory google along with all the files, under include, and paste it in a directory inside your go workspace, for easy accessibility. Refer: Fig 3![Fig 2.](//images.ctfassets.net/urdv1oztwvyo/1mE53Z0AOsrnVDxUgmTFUJ/2e23ed91b7b00484e67547c3f3adb8fc/1_ASsTIuN2-jDhtboDXV3jPw.png)![Fig 3](//images.ctfassets.net/urdv1oztwvyo/GGvVEjZGjEtSvLqzAwLMd/403c7d24197bf963c1596a833049d047/1_q12wQi7Yin3AHDKyK-EkPQ.png)Just one step away from writing our service in go. Yeah!Type the following command on your terminal:```bashprotoc --proto_path=proto --proto_path=extras --go_out=plugins=grpc:proto server.proto```And…KaBoom!! You’ll find your server.pb.go file under proto directory that does all the magic. (Refer Fig 3)Time to write the simple go service![Fig 4: main.go](//images.ctfassets.net/urdv1oztwvyo/2DywwioLYkWac0qgI0sUHO/95f19df7758c4700b4611a2c7bda75ca/1_4vKpkPyNCavydSIFZyePAA.png)Now our server is ready to serve our flutter client, with its simple Add service.# ClientBefore setting up gRPC for dart please ensure that:1. You have Flutter SDK set up in your android studio.2. You have installed dart on your systemRefer the links to do the same if not already done https://appitventures.com/blog/how-to-setup-flutter-on-android-studio/Let’s setup our gRPC clientBefore we get into coding our flutter app, lets get the protoc plugin activated for dart which will generate dart based class files corresponding to our server.proto file, using a simple command on your terminal:`pub global activate protoc_plugin`The compiler plugin, protoc-gen-dart, is installed in $HOME/.pub-cache/bin. It must be in your $PATH for the protocol compiler, protoc, to find it.`export PATH=$PATH:$HOME/.pub-cache/bin`*Note: pub command won’t be recognised as a command until you install dart*Time to create a Flutter project in Android StudioIn your Android Studio create a new flutter project (File ->New->New Flutter Project)This will generate a main.dart file under lib directory by default. (I have just edited the same file in order to call the service for a while)Add server.proto file to your flutter projectCreate a directory proto in your project and add server.proto file here as shown in figure below:![Fig 5: Pasting the same server.proto file to our Flutter project](//images.ctfassets.net/urdv1oztwvyo/1OOVBczQVPKvIhDC5Q8P85/3df9b3d29a1aa59cb8eef63ee0b012e4/1_ssikelPMOG3lPJSskZ9y1w.png)Let’s generate our magic files for dart corresponding to the protoJust type the following command in your terminal to find your four magic dart files, which exposes the server’s addService to the client(Refer Fig 6):```bashprotoc --proto_path=$HOME/Code/flutter_grpc_demo/proto --dart_out=grpc:lib/service $HOME/Code/flutter_grpc_demo/proto/server.proto```The command declares a path for our proto file server.proto and ask dart to output the class files in lib/service directory.![Fig 6: The four dart files built corresponding to our proto file](//images.ctfassets.net/urdv1oztwvyo/4WS8X2wjIBSvTvahjMTLVX/c43a21358ecec4e6fc2fb1c6710419e9/1_qHnWYkDCSyhYT6LV2a9JFA.png)Almost Done! Let’s write a client and a method to bind onto add service# Service MethodI have created a class MathService in math_service.dart file (Refer Fig 6 and Fig 7) and defined an async add method which will be exposed to our flutter application in order to receive two numbers as input on user’s action, and will await the server response and return the same after every call.![Fig 7: math_service.dart](//images.ctfassets.net/urdv1oztwvyo/SnSC22thLMaaUTw4uEE70/2564e8044aefb3b9120fe8cdda40b930/1_8MD6jVcPBBTkwUmFAYVnfA.png)# gRPC FlutterClientThe following is a simple gRPC client, that has a channel set up for our server listening at port 4040.![Fig 8: GrpcFlutterClient.dart](//images.ctfassets.net/urdv1oztwvyo/5RpWebXYhZvVjiIiu0q4c3/60b3abbbd5d3649170956a4d05c98e17/1_gRmfcmBet6lGdSC1rGrDnA.png)Final Step! Editing main.dart file to call the serviceCan’t wait? Same here!Before that…Your pubspec.yaml file should look like the following:![Fig 9: Add dependencies for grpc and protobuf](//images.ctfassets.net/urdv1oztwvyo/K1wlqEfIUCENEeOFP2kUJ/1a4f7871dcec05ae3cef8a7f1611046a/1_cqpCS-XjxKVzusQWvAVv5w.png)After a bit of editing for the purpose of adding our service request our main.dart file looks like this:![Fig 10: Final main.dart](//images.ctfassets.net/urdv1oztwvyo/1siHhMvlpmm4oPkRyeF4dR/934619a2ac2b8dc8485290933ba2dded/1_kHjOXwKEmzgI5SVzsjToHw.png)The addNumbers method is an async method that is called whenever the user taps the floating action button, and increments the number on screen by 5. The counter keeps on updating as and when we receive response after addition operation from the server.Let’s run the app nowOnce we run the app, the app looks like the following by default:![Fig 11: Flutter App on emulator](//images.ctfassets.net/urdv1oztwvyo/1iHQVW8C3NdxD42Ez2Xvan/d4870bde04cb7e0fc7dca57eea6fb1d0/1_lF77k80VpEaNu20ZNKtYFw.png)Let’s start our server with the following command in order to respond when user taps on the button:```golanggo run main.go```Now tap on the button and get the number on the screen incremented by 5, every time you tap the button.Following is a small demonstration of our client and server in action:![Fig 12 : The Flutter gRPC client and goLang gRPC server in action](//images.ctfassets.net/urdv1oztwvyo/6GSGBfUSvlUCcJQ0Ua4yXC/e5882527dae8cb949e78ea0dacb265dd/1_YDQ9N5Z7g2PtGphjJkVOxg.gif)I hope the above work makes it easy for us to start right away with flutter and gRPC together.Please support by applauding and sharing if you found it helpful. Looking forward to writing more on similar topics and explorations.References:https://www.youtube.com/watch?v=Y92WWaZJl24https://medium.com/flutter-community/flutter-grpc-810f87612c6d