Skip to content

Experimental: Launch Flutter with non main entrypoint

Matt Carroll edited this page Apr 8, 2019 · 2 revisions

Everything in this doc and linked from this doc is experimental. These details WILL change. Do not use these instructions or APIs in production code because we will break you.

Launch Flutter with non-main entrypoint

Typically, a Flutter app begins execution at the Dart method called main(), however this is not required. Developers can specify a different Dart entrypoint:

FlutterActivity

To use a FlutterActivity with a custom Dart entrypoint, do the following.

// Launches FlutterActivity and runs a Dart method called mySpecialScreen().
Intent defaultFlutter = new FlutterActivity.IntentBuilder()
  .dartEntrypoint("mySpecialScreen")
  .build(currentActivity);
startActivity(defaultFlutter);

FlutterFragment

To use a FlutterActivity with a custom Dart entrypoint, do the following.

// Create a FlutterFragment that runs a Dart method called mySpecialScreen().
FlutterFragment flutterFragment = new FlutterFragment.Builder()
  .dartEntrypoint("mySpecialScreen")
  .build();

FlutterView

To use a FlutterView with a custom Dart entrypoint, do the following.

// Configure your entrypoint.
DartExecutor.DartEntrypoint entrypoint = new DartExecutor.DartEntrypoint(  
  getResources().getAssets(),
  FlutterMain.findAppBundlePath(getContext()),
  'mySpecialScreen',
);

// Create a new FlutterEngine to run your entrypoint.
FlutterEngine engine = new FlutterEngine();
engine.getDartExecutor().executeDartEntrypoint(entrypoint);

// Connect your FlutterEngine to your FlutterView.
flutterView.attachToFlutterEngine(engine);

Avoid Tree Shaking in Release

When you build in release mode, your Dart code is tree-shaken. This means that the compiler removes any Dart code that it thinks you're not using. This includes your special entrypoints. To avoid crashing in release mode as a result of tree-shaking, be sure to place the following @pragma above each of your custom entrypoints.

@pragma('vm:entry-point')
void mySpecialScreen() {
  // implementation
}

Flutter Wiki

Process

Framework repo

Engine repo

Infrastructure

Experimental features

Release Notes

Clone this wiki locally
You can’t perform that action at this time.