Skip to content

Create a hover compatible plugin

Pierre Champion | Drakirus edited this page Mar 21, 2020 · 11 revisions

Add go-flutter support to a flutter plugin

In this tutorial will get the most simple platform plugin up and running using go-flutter.

TL;DR:

flutter create --template=plugin test_hover
cd test_hover
hover init-plugin github.com/my-organization/test_hover
cd example
flutter build bundle
hover init
hover plugins get
yes | hover run

After running flutter create --org com.example --template=plugin test_hover.
You have a newly created flutter plugin project in the test_hover/ folder with the specialized android/ios code related to the plugin.

Initialize a go-flutter plugin

To add support for go-flutter, you need to run hover init-plugin github.com/my-organization/test_hover.
The Github VCS URL is crucial, as this is where your plugin will be published and fetch by the golang tool-chain.

Once initialized the directory of your plugin should look something like this:

$ ls
android  CHANGELOG.md  go  test_hover.iml  ios  lib  LICENSE  pubspec.yaml  README.md  test

Under the go directory, you will find the following hover created files:

$ ls go
dlib  go.mod  go.sum  import.go.tmpl  plugin.go  README.md

Each file have a specialized purpose:

  • plugin.go is where your platform plugin code lives.
  • import.go.tmpl is used by hover to import your plugin (hover plugins get).

    ⚠️ This file will be copied to the flutter project and is responsible for importing the plugin.
    Plugin makers are encouraged to tweak this file to their needs!

  • go.mod and go.sum are used by the golang tool-chain for versioning.
    More information about 'go modules' is available on the golang wiki.
  • The dlib folder is used for the plugins which use CGO dynamic libraries.

Test the plugin

Once generated, you can test the plugin in the example directory:

  • cd example to go to the flutter app that depends on the plugin, and illustrates how to use it.
  • hover init to initialize go-flutter.
  • hover run to compile and run your application.
    • At that point, the application display "Running on: Unknown".
      The go plugin generated by hover init-plugin XX isn't added to the project.
  • hover plugins get to import the plugin.
    • hover support two types of plugin, 'hosted' one and 'path' one declared respectively in pubspec.yaml as:
      # hosted plugin
      test_hover: ^M.m.p
      and
      # path plugin
      test_hover:
        path: ../
  • hover run, the application should display "Running on: go-flutter vX.X.X".

Everything is working! You can go and modify the content of lib/test_hover.dart and go/plugin.go.

Note: multiples hover plugins commands are available, read more here.

Publish your plugin

Once you are happy with your plugin, you can deploy it as a hosted one.

To do so, go to the root of your plugin (cd .. if you where in the example directory), and run hover publish-plugin.

To successfully publish your plugin, you need to create a git tag.
To automate this process, hover publish-plugin can be used, publish-plugin ensure that your go directory is clean and that your remote git URL matches the VSC URL defined in go/go.mod.

Note

The rule of thumb to use golang module in the context of go-flutter is:

  • The VCS URL should always point to a directory containing a go.mod file.
  • The tag name of the git tag should be 'go/0.0.1' with '0.0.1' the string corresponding to the version number in the pubspec.yaml file. And 'go' the path from the root of your repo to the go.mod file.
    • For example, in the sqflite plugin, the tag should be sqflite/go/1.1.7+2.