Flutter: Simple example of executing Dart/Flutter code from native
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.