Tutorial - Using Protobuf in Flutter

Tutorial - Using Protobuf in Flutter

Recently, I have working on Futter project. However, I have found it quite difficult to install the Dart protobuf plugin on Linux. Here is my tutorial for you to save your time.

  1. Install Dart

    Following the instruction here to install Dart

    Dart SDK

    For me, I just download the deb file and install it.

    sudo dpkg -i dart_2.1.0-1_amd64.deb

    After that we can use dart in terminal, but we cannot find pub . The reason is beacuse it is located in usr/lib/dart/bin Therefore, we’d better change the PATH permanently.

  1. Change the PATH permanently for pub

    gedit ~/.profile

    Then add the following to the end of .profile

    export PATH=$PATH:/usr/lib/dart/bin

    Logout and login again to let the modification take effect. Type pub in terminal, we will see it works.

  1. Install protoc

    The simplest way is to use to precompiled protoc binary and put it into PATH

    Protobuf Release

    For me, I download the protoc-3.6.1-osx-x86_64.zip. Unzip it, and put protoc binary to /usr/bin

    Then, we type protoc in terminal, we will see it works.

  1. Install Dart protoc_plugin

    In the terminal, type

    pub global activate protoc_plugin

    Right now, I’m in China. For reasons known to all, I have to use the mirror

    export PUB_HOSTED_URL=https://pub.flutter-io.cn

    However, it is installed in $HOME/.pub-cache/bin. We’d better make it into PATH again.

    Using the same way in ~/.profile

    Now, the last line looks like

    export PATH=$PATH:/usr/lib/dart/bin:$HOME/.pub-cache/bin

    You need to logout and login to again to take effect.

  1. Compile from .proto file

    Finally, we can compile .proto file to .dart file.

    For me, I just go to the directory containing the .proto file, using the command below, and it will generate the .dart file(s) you need. You can copy them to your flutter project

    protc --dart_out=. ./YourFile.proto
  2. Install protobuf runtime library

    Don’t forget to install the runtime library by adding protobuf: ^0.11.0 in your dependency part of pubspec.yaml

That’s it! Hope it will save your time to build your flutter project with awesome protobuf!

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now