swiftflutterflutter-add-to-app

Undefined symbol: _FlutterDefaultDartEntrypoint - Adding a Flutter screen to an iOS app and using initialRoute


The official Adding a Flutter screen to an iOS app documentation suggests that starting with Flutter version 1.22, an initial route can be set for Flutter when constructing the FlutterViewController:

let flutterEngine = FlutterEngine()
// FlutterDefaultDartEntrypoint is the same as nil, which will run main().
engine.run(
  withEntrypoint: FlutterDefaultDartEntrypoint, initialRoute: "/onboarding")

But when following these instructions, the build fails with an error:

Undefined symbol: _FlutterDefaultDartEntrypoint

Solution

  • As the comment in the above code example suggests, "FlutterDefaultDartEntrypoint is the same as nil, which will run main".

    So substituting FlutterDefaultDartEntrypoint with nil solves the issue:

    let flutterEngine = FlutterEngine()
    engine.run(withEntrypoint: nil, initialRoute: "/onboarding")
    

    For running anything other than the default main() method, the following can be used:

    let flutterEngine = FlutterEngine()
    engine.run(
        withEntrypoint: "otherMain",
        libraryURI: "other_file.dart",
        initialRoute: "/onboarding"
    )
    

    This will look for an otherMain() method in the other_file.dart file.