Using Go Library in Flutter

Using Go Library in FlutterArchan PaulBlockedUnblockFollowFollowingMay 14This article is originally published here.

Flutter has made it possible to develope cross platform mobile app with native performance.

Till we had Flutter we used to write the platform specific UI code of mobile app using Java/Kotlin for Android and Objective-C/Swift for iOS.

Now with Flutter we can use same codebase for App/UI for multiple platforms.

While working on various full-stack mobile-app development projects at Arputer Technologies we wrote tons of platform independent business-logic or algorithm code in Go and used Google’s Gomobile tool to create Android/iOS natively compiled libraries which are called from Java/Kotlin or Objective-C/Swift UI depending on the platform.

This approach helped us to write (and test) core business logic code once and use the same in multiple platforms (Android, iOS and .

Linux, Mac).

While working with Flutter we wanted to reuse the same Go codebase/API and we found it easy to use the Gomobile generated Android/iOS libraries in Flutter using Flutter platform-channel.

In this article we will learn how to integrate Go library code into Flutter and use it in cross platform scenario.

We will try to achieve following :A simple Go app which receives an integer variable from Flutter through platform-channel and returns an incremented value of it.

A simple flutter app which sends integer to native Go API and update it’s stateful widget.

We will modify the wellknow counter app which is generated from template when you create Flutter app.

In this article we will assume that the development workstation is Linux and running any of the latest Linux distributions and Android is used for testing the mobile app to be developed.

Development workstation setup :Ensure that Go compiler is installed.

eg: go 1.

12.

2 linux/amd64Ensure that Android SDK is installed.

eg: Android 9.

0 (Pie) – Android SDK Platform 28.

Ensure that Android NDK is installed.

eg: NDK 19.

2.

xAndroid Emulator is installed for testing the app.

eg: Google Play Intel x86 Atom System Image.

Environment setup for Go :First we need to prepare the environment for Gomobile development.

Latest version of Go is recommended to be installedEnvironment variables :Update environment variables (your path might be different, hence update accordingly).

Please note for gomobile to work correctly, you need have above environment variables set and exported.

Gomobile installation :Environment setup for flutter :Create our Go Library for FlutterCreate $GOPATH/src/gonative-lib/data-processor.

goCompile and create Android library :Following files are generated by Gomobile command.

gonativelib.

aar : Android library to be used in Flutter app.

Gonativelib : class corresponding to the Go package.

DataProcessor : class corresponding to Go struct, which is to be used in app.

gonativelib-sources.

jar : Java sources for reference.

Data types supported by Gomobile :At present, a subset of Go types are supported by Gomobile.

For more information refer to gobind.

Signed integer and floating point types.

String and boolean types.

Byte slice types.

Note that byte slices are passed by reference, and support mutation.

Any function type all of whose parameters and results have supported types.

Functions must return either no results, one result, or two results where the type of the second is the built-in ‘error’ type.

Any interface type, all of whose exported methods have supported function types.

Any struct type, all of whose exported methods have supported function types and all of whose exported fields have supported types.

Create Flutter appCreate flutter app from from default template.

When you run this default app from your favorite IDE you should be able to see the familiar app with stateful widget containing the floating-button.

On clicking the floating-button the counter value get updated and shown in UI.

In the default app, the counter is updated in _incrementCounter()We will updated this function to call Go Library API in following sections.

Add Go library (Android) to Flutter app :First, add Android gonativelib which we have created in the previous step, to the Flutter app.

To add the Android gonativelib which we have created in the previous step, to the Flutter app, create libs folder in Android code of Flutter app and copygonativelib Android aar file into this folder.

Now update ~/workspace/flutter_gonative_app/android/app/build.

gradle with following :Corresponding git diff flutter_gonative_app/android/app/build.

gradle.

Now the classes provided by gonativelib should be accessible from Android.

Update ~/workspace/flutter_gonative_app/android/app/src/main/java/com/example/flutter_gonative_app/MainActivity.

java with following to access gonativelib.

DataProcessor.

Corresponding git diff flutter_gonative_app/android/app/src/main/java/com/example/flutter_gonative_app/MainActivity.

java.

Restart your flutter app to get it recompiled with the changes you have made.

Add platform-channel to Flutter appTo call API of gonativelib, we need to use FLutter Platform Channel.

Add MethodChannel in Flutter/Dart :Flutter app’s _MyHomePageState class holds apps's current state.

We need to create a MethodChannel though which we will be communicating with gonalivelib.

The client and host sides of the Channel do the handshake through the channel name passed in the MethodChannel constructor of platform-channel.

In our case, the channel name is example.

com/gonative.

This should be unique in the app.

In the Flutter side update _incrementCounter() function from ~/workspace/flutter_gonative_app/lib/main.

dartCorresponding git diff flutter_gonative_app/lib/main.

dart.

Let us now understand the magic happening in above code.

Once the MethodChannel is established, Flutter can invoke a method (using invokeMethod()) by specifying a concrete method to call via String identifier (in our case dataProcessor_increment).

The invokeMethod returns a Future and may result an exception (hence wrapped in a try-catch block).

Once the Future returns, _counter is updated in setState() call which in turn updates the Widget automatically.

Please note, our gonativelib can return exception which is processed in Android/Java side and transparently forwarded to Flutter/Dart.

The same exception can be addressed in on PlatformException catch (e).

Add MethodChannel in Android/Java :Now in Android Java side update MainActivity.

onCreate at ~/workspace/flutter_gonative_app/android/app/src/main/java/com/example/flutter_gonative_app/MainActivity.

java with following :Corresponding git diff flutter_gonative_app/android/app/src/main/java/com/example/flutter_gonative_app/MainActivity.

javaIn the Android/Java side, we have added MethodCallHandler to the methodChannel to handle method calls received from Flutter/Dart.

onMethodCall handles specified method call received from Flutter along with arguments.

The argument is recommended to be a Map in Flutter/Dart which can be decoded in Android/Java side as shown above.

For more information about argument check method-details documentation.

Now, restart the app and when you click the floating-button, Flutter/Dart calls MethodChannel to invokeMethod with current state's _counter.

On receiving the call in though predefined MethodChannel, Android/Java calls corresponding GoNative object to get the output and returns the same to Flutter.

End noteIn this article I have used a trivial example to demonstrate how to use Flutter’s platform-channel to call GoNative api in the context of Android.

Similar approach can be used in case of iOS.

What we learned in this article :Developing Go library API which can be source compiled for cross platforms.

Integration of Go library into Flutter app (Android context).

Use platform channel in Flutter app to call platform specific libraries (in our case Go native library).

Send argument(s) to Go API from Flutter and return (synchronous) results back to Flutter for UI update.

The source code can be found from Bitbucket/arputer.

In the future article in this series I will try to demonstrate an example about, how to use RxDart and EventChannel to achieve this.

Stay tuned.

.

. More details

Leave a Reply