Sub topic 8
Choosing a Dart Entrypoint and Initial Route
This chapter assumes that you know basic Dart and Flutter, can run a Flutter app with the flutter command, and understand either Android Activities or iOS view controllers. It also assumes that your native app already has a Flutter module or that you are following the module setup from the previous chapter.
When a native app opens Flutter, it must choose two related values:
- A Dart entrypoint is a Dart function where the Flutter isolate starts.
- An initial route is the first route that Flutter displays after the entrypoint starts.
The default entrypoint is main(). A second entrypoint lets the same Flutter module start with a different application configuration.
Defining multiple entrypoints
An entrypoint is an ordinary top-level Dart function. It must call runApp, just like main() does.
Use @pragma('vm:entry-point') on an entrypoint that native code will start by name. This annotation prevents release-build tree shaking from removing the function when Dart code does not call it directly.
import 'package:flutter/material.dart';
void main() => runApp(const ModuleApp());
@pragma('vm:entry-point')
void cartMain() {
runApp(const ModuleApp(initialRoute: '/cart'));
}
class ModuleApp extends StatelessWidget {
const ModuleApp({super.key, this.initialRoute = '/'});
final String initialRoute;
@override
Widget build(BuildContext context) {
return MaterialApp(
initialRoute: initialRoute,
routes: {
'/': (_) => const HomePage(),
'/cart': (_) => const CartPage(),
},
);
}
}
class HomePage extends StatelessWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context) {
return const Scaffold(
body: Center(child: Text('Home')),
);
}
}
class CartPage extends StatelessWidget {
const CartPage({super.key});
@override
Widget build(BuildContext context) {
return const Scaffold(
body: Center(child: Text('Cart')),
);
}
}The default main() starts the home experience. cartMain() starts the same application with /cart as its initial route. Both functions use the same widget tree, but they choose different startup behavior.
Entrypoint and route are different choices
The entrypoint controls how the Dart application is initialized. The initial route controls which screen the initialized application displays.
You can use one entrypoint with different routes when the startup setup is the same. For example, a native host might always start main() but select /profile or /settings. Use separate entrypoints when the startup work or dependency setup differs.
Starting the selected entrypoint
An engine can run only one Dart entrypoint for its isolate. Select the entrypoint when you create and run a new engine. If you pre-warm an engine and place it in a cache, that engine keeps the entrypoint it already started.
On Android, a new engine can be configured with a named Dart entrypoint and an initial route. The exact builder methods can vary with the Flutter embedding version, so check the API available in your project. The important rule is that the route and entrypoint must be selected before the engine starts.
On iOS, the equivalent operation is running the engine with an entrypoint and initial route:
import Flutter
let engine = FlutterEngine(name: "cart_engine")
engine.run(
withEntrypoint: "cartMain",
initialRoute: "/cart"
)After the engine is running, attach a FlutterViewController to it:
let flutterViewController = FlutterViewController(
engine: engine,
nibName: nil,
bundle: nil
)
present(flutterViewController, animated: true)If you reuse a cached engine, do not expect to change its Dart entrypoint by attaching a different view controller. Create another engine when the application needs a different entrypoint.
Common errors
- Omitting
@pragma('vm:entry-point')can make a named entrypoint fail in a release build. - Misspelling the entrypoint name causes the native engine to fail before Flutter shows a screen.
- Passing a route that is not registered produces an unknown-route error or fallback behavior.
- Trying to change the entrypoint of a running cached engine does not restart its Dart isolate.
- Starting an engine with
/cartand later assuming it can become a completely separate Flutter application can create state and navigation bugs.
Use a naming convention for entrypoints, register every supported route, and decide whether each screen needs a new engine or can share an existing application.
Exercise
You need two native buttons that open the same Flutter application at /home and /settings. Do you need two Dart entrypoints?
Show answer
No. One entrypoint can start the application, while the native host selects a different initial route for each newly configured Flutter experience.