Ognjen Regoje bio photo

Ognjen Regoje
But you can call me Oggy


I make things that run on the web (mostly).
More ABOUT me and my PROJECTS.

me@ognjen.io LinkedIn

Flutter: Simple example of executing Dart/Flutter code from native

#flutter #technical

Previously I’ve written how to pass messages between native code and Dart. Sometimes though you might want to just execute code and don’t care about getting the result back. This was trickier than I thought it should be, and it’s not covered explicitly in documentation, other then the geofencing example that I found less then ideal.

After a bit of tinkering and experimenting I finally figured out what the simplest way is, even though I’m not entirely sure if this is the correct way.

In Java, you’d do:

FlutterNativeView sBackgroundFlutterView = new FlutterNativeView(context, true);
FlutterRunArguments args = new FlutterRunArguments();
args.bundlePath = FlutterMain.findAppBundlePath(context);
args.entrypoint = "background_execute";

Where background_execute is the name of the method in main.dart that would be executed, and it’d look like this:

void background_execute(){
  return true;
}

Quite simple.