From e50dccbae545b830be356ad599037fe46ed3387b Mon Sep 17 00:00:00 2001 From: Xiao Yu Date: Fri, 4 Sep 2020 23:29:49 -0700 Subject: [PATCH] Fix initial route documentation for iOS --- .../add-to-app/ios/add-flutter-screen.md | 53 +++++++++++-------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/src/docs/development/add-to-app/ios/add-flutter-screen.md b/src/docs/development/add-to-app/ios/add-flutter-screen.md index 0665b243e4b..095408fb392 100644 --- a/src/docs/development/add-to-app/ios/add-flutter-screen.md +++ b/src/docs/development/add-to-app/ios/add-flutter-screen.md @@ -422,45 +422,52 @@ flutterEngine.run(withEntrypoint: "myOtherEntrypoint", libraryURI: "other_file.d ### Route -An initial route can be set for your Flutter [`WidgetsApp`][] -when constructing the engine. +Starting in Flutter version 1.22, an initial route can be set for your Flutter +[`WidgetsApp`][] when constructing the FlutterEngine or the +FlutterViewController. {% samplecode initial-route %} {% sample Objective-C %} ```objectivec -FlutterEngine *flutterEngine = - [[FlutterEngine alloc] initWithName:@"my flutter engine"]; -[[flutterEngine navigationChannel] invokeMethod:@"setInitialRoute" - arguments:@"/onboarding"]; -[flutterEngine run]; +FlutterEngine *flutterEngine = [[FlutterEngine alloc] init]; +// FlutterDefaultDartEntrypoint is the same as nil, which will run main(). +[flutterEngine runWithEntrypoint:FlutterDefaultDartEntrypoint + initialRoute:@"/onboarding"]; ``` {% sample Swift %} ```swift -let flutterEngine = FlutterEngine(name: "my flutter engine") -flutterEngine.navigationChannel.invokeMethod("setInitialRoute", arguments:"/onboarding") -flutterEngine.run() +let flutterEngine = FlutterEngine() +// FlutterDefaultDartEntrypoint is the same as nil, which will run main(). +engine.run( + withEntrypoint: FlutterDefaultDartEntrypoint, initialRoute: "/onboarding") ``` {% endsamplecode %} This code sets your `dart:ui`'s [`window.defaultRouteName`][] to `"/onboarding"` instead of `"/"`. -{{site.alert.warning}} - `"setInitialRoute"` on the `navigationChannel` must be called - before running your `FlutterEngine` in order for Flutter's - first frame to use the desired route. +Alternatively, to construct a FlutterViewController directly without pre-warming +a FlutterEngine: - Specifically, this must be called before running the Dart entrypoint. - The entrypoint may lead to a series of events where - [`runApp`][] builds a Material/Cupertino/WidgetsApp - that implicitly creates a [Navigator][] that might - `window.defaultRouteName` when the [`NavigatorState`][] is - first initialized. - - Setting the initial route after running the engine doesn't have an effect. -{{site.alert.end}} +{% samplecode initial-route %} +{% sample Objective-C %} + +```objectivec +FlutterViewController* flutterViewController = + [[FlutterViewController alloc] initWithProject:nil + initialRoute:@"/onboarding" + nibName:nil + bundle:nil]; +``` +{% sample Swift %} + +```swift +let flutterViewController = FlutterViewController( + project: nil, initialRoute: "/onboarding", nibName: nil, bundle: nil) +``` +{% endsamplecode %} {{site.alert.tip}} In order to imperatively change your current Flutter