Sub topic 3
Choosing a Dart entrypoint and initial route
This chapter assumes you have a Flutter module running in a native app, you know Flutter routes, and you can read Kotlin or Swift. It also assumes you know the basics of Gradle or CocoaPods.
Choosing a Dart entrypoint and initial route
A Dart entrypoint is the function the Flutter engine calls first. The default entrypoint is main(). Any other top-level function marked with @pragma('vm:entry-point') is also an entrypoint, and the compiler keeps it. The native host names the function it wants when it starts the engine. The initial route is the screen a MaterialApp shows first.
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 Scaffold(body: Center(child: Text('Home'))),
'/cart': (_) => const Scaffold(body: Center(child: Text('Cart'))),
},
);
}
}On Android, the host selects cartMain with new DartExecutor.DartEntrypoint("lib/main.dart", "cartMain").
Debugging an embedded Flutter module
Run the host app in debug mode and open a Flutter screen. Then attach the tooling to the running engine from the module directory:
cd my_flutter_module
flutter attachHot reload (press r) and DevTools work after the attach succeeds. Attach only works while the Flutter engine is running.
Packaging for release: source, AAR, and frameworks
| Option | Command or method | Best for |
|---|---|---|
| Source | Host includes the module through Gradle or CocoaPods | Teams that build the host and module together |
| Android AAR | `flutter build aar` | Android hosts that consume a published artifact |
| iOS XCFramework | `flutter build ios-framework --output=build/ios/framework/` | iOS hosts that use prebuilt binaries |
Generating type-safe interfaces with Pigeon
Platform channels pass untyped messages, so a misspelled method name fails only at runtime. Pigeon is a code generator. You describe the messages and the API in one Dart file, and Pigeon writes matching Dart, Kotlin, and Swift code. Both sides then share the same types, and the compiler catches mismatches.
Several Flutter screens with FlutterEngineGroup
Each FlutterEngine uses memory. FlutterEngineGroup creates engines that share resources, so extra engines start faster and use less memory. Each engine can run a different entrypoint.
Limitations and trade-offs to plan for
- Each live engine adds memory use, so keep the number of engines small.
- Flutter screens take longer to start than native screens unless the engine is warmed up in advance.
- The host owns navigation between native and Flutter screens.
- Flutter state is lost when the host releases its engine.
- Native and Flutter teams must agree on the Flutter SDK version.
Exercise
Your Android host must open a Flutter cart screen that starts at /cart. Which entrypoint name do you pass to DartEntrypoint, and what must the Dart function include?
Show answer
Pass "cartMain", the top-level function that calls runApp with initialRoute: '/cart'. The function must have the @pragma('vm:entry-point') annotation so the compiler keeps it in release builds.