Working With gRPC in Flutter on Top of a Go Service

Repository

https://github.com/flutter/flutter

What Will I Learn?

  • How to use gRPC in Flutter
  • How to implement a gRPC client with Dart
  • How to generate code from ProtoBufs for Dart
  • How to deal with Streaming Services from gRPC
  • How to interface a gRPC format with Flutter
  • You will learn how to contact the localhost from an emulator

Requirements

System Requirements:
OS Support for Flutter:
  • Windows 7 SP1 or later (64-bit)
  • macOS (64-bit)
  • Linux (64-bit)

Required Knowledge

  • A basic knowledge Programming
  • A fair understanding of Mobile development and Imperative or Object Oriented Programming
  • Basic knowledge of dependencies

Resources for Flutter and this Project:

Sources:

Flutter Logo (Google): https://flutter.io/

Difficulty

  • Intermediate

Description

In this Flutter Video Tutorial, we take a look at how we can neatly interface a gRPC service from any other language and create a gRPC client. This includes generating the code we need from the Protocol Buffer files and using those generated files to build our client in Flutter. Also, because the Go service streams data to the client; we look at how we can build a streaming client in Flutter.

gRPC and Protocol Buffers with Dart and Other Languages

Protocol buffers are a language neutral and platform neutral serialization mechanism. In some previous tutorials, we've looked at using gRPC and Protocol Buffers with the Go programming language. In one of those examples we built out a small little chat application which is the basis for this tutorial. We can leverage the existing gRPC interface to pass data back and forth between the Go server and a newly build Flutter Client.


protobuf.png

The protobuffer file above is used to generate four dart files by way of the Dart protoc_plugin and the protoc compiler. The files generated are a service.pb.dart file, a service.pbenum.dart file, a service.pbgrpc.dart file and a service.pbjson.dart file. For this tutorial, we are interested in only two of the files that are generated; the main service.pb.dart file and the service.pbgrpc.dart file. The former file contains generic information about the data structures and service functions which we defined in the protobuffer file while the service.pbgrpc.dart file gives us access to gRPC specific features for our client such as instructions for how to serialize the data. The service.pbenum.dart file would help us if we had enumerated data structures in our protobuffer and the service.pbjson.dart file would be useful if we wanted to use a JSON serialization format for our RPCs.

Constructing a gRPC Client with Flutter and Dart

Since Flutter allows us to build mobile applications, we are able to define backend services through the gRPC protocol as an alternative to using a traditional web API. In this case, because our application uses realtime features, the gRPC protocol gives us more performance and speed for the chat application. The Go service holds the state for the chat service and is able to pass that data back to each of the clients that are connected to it. To make this work, we need to define the proper functionality in our Flutter application's logic.


client.png

In the tutorial, we put all of the logic into a ChatService object. This object contains functionality to connect to the Go service as well as functionality to send the appropriate data types along the connection. When defining a datastructure from the generated files, we need to specifically generate the structure. Notice the use of the cascade operator or double dot operator. This operator is used to make the code less verbose and allow us to append data onto the bare bones user and message objects. The Dart gRPC library is what gives us access to the data types that will let us connect to our Go service; data types like the ChannelOptions and the ClientChannel Object.

The Source Code for this video may be found here: https://github.com/tensor-programming/flutter-grpc-client-example
The Source Code for the original Go Service can be found here: https://github.com/tensor-programming/docker_grpc_chat_tutorial

Video Tutorial

Curriculum

Related Videos

Projects and Series

Stand Alone Projects:
Utopian Rocks Example
Building a Calculator
Movie Searcher Application

Minesweeper Game

Redux Todo App

RxCommand Weather App

Curriculum

Proof of Work Done

https://github.com/tensor-programming

H2
H3
H4
3 columns
2 columns
1 column
7 Comments