Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 8d08e6c

Browse files
authored
Refactor initial route code (#19684)
1 parent ccfc7e5 commit 8d08e6c

File tree

21 files changed

+353
-133
lines changed

21 files changed

+353
-133
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,7 @@ FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine.
925925
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterEnginePlatformViewTest.mm
926926
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterEngineTest.mm
927927
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine_Internal.h
928+
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine_Test.h
928929
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterHeadlessDartRunner.mm
929930
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterObservatoryPublisher.h
930931
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterObservatoryPublisher.mm

lib/ui/window.dart

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,23 +1056,19 @@ class Window {
10561056
///
10571057
/// ## Android
10581058
///
1059-
/// On Android, calling
1060-
/// [`FlutterView.setInitialRoute`](/javadoc/io/flutter/view/FlutterView.html#setInitialRoute-java.lang.String-)
1061-
/// will set this value. The value must be set sufficiently early, i.e. before
1062-
/// the [runApp] call is executed in Dart, for this to have any effect on the
1063-
/// framework. The `createFlutterView` method in your `FlutterActivity`
1064-
/// subclass is a suitable time to set the value. The application's
1065-
/// `AndroidManifest.xml` file must also be updated to have a suitable
1066-
/// [`<intent-filter>`](https://developer.android.com/guide/topics/manifest/intent-filter-element.html).
1059+
/// On Android, the initial route can be set on the [initialRoute](/javadoc/io/flutter/embedding/android/FlutterActivity.NewEngineIntentBuilder.html#initialRoute-java.lang.String-)
1060+
/// method of the [FlutterActivity](/javadoc/io/flutter/embedding/android/FlutterActivity.html)'s
1061+
/// intent builder.
1062+
///
1063+
/// On a standalone engine, see https://flutter.dev/docs/development/add-to-app/android/add-flutter-screen#initial-route-with-a-cached-engine.
10671064
///
10681065
/// ## iOS
10691066
///
1070-
/// On iOS, calling
1071-
/// [`FlutterViewController.setInitialRoute`](/objcdoc/Classes/FlutterViewController.html#/c:objc%28cs%29FlutterViewController%28im%29setInitialRoute:)
1072-
/// will set this value. The value must be set sufficiently early, i.e. before
1073-
/// the [runApp] call is executed in Dart, for this to have any effect on the
1074-
/// framework. The `application:didFinishLaunchingWithOptions:` method is a
1075-
/// suitable time to set this value.
1067+
/// On iOS, the initial route can be set on the `initialRoute`
1068+
/// parameter of the [FlutterViewController](/objcdoc/Classes/FlutterViewController.html)'s
1069+
/// initializer.
1070+
///
1071+
/// On a standalone engine, see https://flutter.dev/docs/development/add-to-app/ios/add-flutter-screen#route.
10761072
///
10771073
/// See also:
10781074
///

lib/web_ui/lib/src/ui/window.dart

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -710,26 +710,6 @@ abstract class Window {
710710
///
711711
/// This will be the string "`/`" if no particular route was requested.
712712
///
713-
/// ## Android
714-
///
715-
/// On Android, calling
716-
/// [`FlutterView.setInitialRoute`](/javadoc/io/flutter/view/FlutterView.html#setInitialRoute-java.lang.String-)
717-
/// will set this value. The value must be set sufficiently early, i.e. before
718-
/// the [runApp] call is executed in Dart, for this to have any effect on the
719-
/// framework. The `createFlutterView` method in your `FlutterActivity`
720-
/// subclass is a suitable time to set the value. The application's
721-
/// `AndroidManifest.xml` file must also be updated to have a suitable
722-
/// [`<intent-filter>`](https://developer.android.com/guide/topics/manifest/intent-filter-element.html).
723-
///
724-
/// ## iOS
725-
///
726-
/// On iOS, calling
727-
/// [`FlutterViewController.setInitialRoute`](/objcdoc/Classes/FlutterViewController.html#/c:objc%28cs%29FlutterViewController%28im%29setInitialRoute:)
728-
/// will set this value. The value must be set sufficiently early, i.e. before
729-
/// the [runApp] call is executed in Dart, for this to have any effect on the
730-
/// framework. The `application:didFinishLaunchingWithOptions:` method is a
731-
/// suitable time to set this value.
732-
///
733713
/// See also:
734714
///
735715
/// * [Navigator], a widget that handles routing.

shell/platform/darwin/ios/framework/Headers/Flutter.h

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,52 +5,6 @@
55
#ifndef FLUTTER_FLUTTER_H_
66
#define FLUTTER_FLUTTER_H_
77

8-
/**
9-
BREAKING CHANGES:
10-
11-
December 17, 2018:
12-
- Changed designated initializer on FlutterEngine
13-
14-
October 5, 2018:
15-
- Removed FlutterNavigationController.h/.mm
16-
- Changed return signature of `FlutterDartHeadlessCodeRunner.run*` from void
17-
to bool
18-
- Removed HeadlessPlatformViewIOS
19-
- Marked FlutterDartHeadlessCodeRunner deprecated
20-
21-
August 31, 2018: Marked -[FlutterDartProject
22-
initFromDefaultSourceForConfiguration] and FlutterStandardBigInteger as
23-
unavailable.
24-
25-
July 26, 2018: Marked -[FlutterDartProject
26-
initFromDefaultSourceForConfiguration] deprecated.
27-
28-
February 28, 2018: Removed "initWithFLXArchive" and
29-
"initWithFLXArchiveWithScriptSnapshot".
30-
31-
January 15, 2018: Marked "initWithFLXArchive" and
32-
"initWithFLXArchiveWithScriptSnapshot" as unavailable following the
33-
deprecation from December 11, 2017. Scheduled to be removed on February
34-
19, 2018.
35-
36-
January 09, 2018: Deprecated "FlutterStandardBigInteger" and its use in
37-
"FlutterStandardMessageCodec" and "FlutterStandardMethodCodec". Scheduled to
38-
be marked as unavailable once the deprecation has been available on the
39-
flutter/flutter alpha branch for four weeks. "FlutterStandardBigInteger" was
40-
needed because the Dart 1.0 int type had no size limit. With Dart 2.0, the
41-
int type is a fixed-size, 64-bit signed integer. If you need to communicate
42-
larger integers, use NSString encoding instead.
43-
44-
December 11, 2017: Deprecated "initWithFLXArchive" and
45-
"initWithFLXArchiveWithScriptSnapshot" and scheculed the same to be marked as
46-
unavailable on January 15, 2018. Instead, "initWithFlutterAssets" and
47-
"initWithFlutterAssetsWithScriptSnapshot" should be used. The reason for this
48-
change is that the FLX archive will be deprecated and replaced with a flutter
49-
assets directory containing the same files as the FLX did.
50-
51-
November 29, 2017: Added a BREAKING CHANGES section.
52-
*/
53-
548
#include "FlutterAppDelegate.h"
559
#include "FlutterBinaryMessenger.h"
5610
#include "FlutterCallbackCache.h"

shell/platform/darwin/ios/framework/Headers/FlutterEngine.h

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ NS_ASSUME_NONNULL_BEGIN
2424
*/
2525
extern NSString* const FlutterDefaultDartEntrypoint;
2626

27+
/**
28+
* The default Flutter initial route ("/").
29+
*/
30+
extern NSString* const FlutterDefaultInitialRoute;
31+
2732
/**
2833
* The FlutterEngine class coordinates a single instance of execution for a
2934
* `FlutterDartProject`. It may have zero or one `FlutterViewController` at a
@@ -53,6 +58,24 @@ extern NSString* const FlutterDefaultDartEntrypoint;
5358
FLUTTER_EXPORT
5459
@interface FlutterEngine : NSObject <FlutterTextureRegistry, FlutterPluginRegistry>
5560

61+
/**
62+
* Default initializer for a FlutterEngine.
63+
*
64+
* Threads created by this FlutterEngine will appear as "FlutterEngine #" in
65+
* Instruments. The prefix can be customized using `initWithName`.
66+
*
67+
* The engine will execute the project located in the bundle with the identifier
68+
* "io.flutter.flutter.app" (the default for Flutter projects).
69+
*
70+
* A newly initialized engine will not run until either `-runWithEntrypoint:` or
71+
* `-runWithEntrypoint:libraryURI:` is called.
72+
*
73+
* FlutterEngine created with this method will have allowHeadlessExecution set to `YES`.
74+
* This means that the engine will continue to run regardless of whether a `FlutterViewController`
75+
* is attached to it or not, until `-destroyContext:` is called or the process finishes.
76+
*/
77+
- (instancetype)init;
78+
5679
/**
5780
* Initialize this FlutterEngine.
5881
*
@@ -114,17 +137,12 @@ FLUTTER_EXPORT
114137
project:(nullable FlutterDartProject*)project
115138
allowHeadlessExecution:(BOOL)allowHeadlessExecution NS_DESIGNATED_INITIALIZER;
116139

117-
/**
118-
* The default initializer is not available for this object.
119-
* Callers must use `-[FlutterEngine initWithName:project:]`.
120-
*/
121-
- (instancetype)init NS_UNAVAILABLE;
122-
123140
+ (instancetype)new NS_UNAVAILABLE;
124141

125142
/**
126143
* Runs a Dart program on an Isolate from the main Dart library (i.e. the library that
127-
* contains `main()`), using `main()` as the entrypoint (the default for Flutter projects).
144+
* contains `main()`), using `main()` as the entrypoint (the default for Flutter projects),
145+
* and using "/" (the default route) as the initial route.
128146
*
129147
* The first call to this method will create a new Isolate. Subsequent calls will return
130148
* immediately and have no effect.
@@ -135,7 +153,7 @@ FLUTTER_EXPORT
135153

136154
/**
137155
* Runs a Dart program on an Isolate from the main Dart library (i.e. the library that
138-
* contains `main()`).
156+
* contains `main()`), using "/" (the default route) as the initial route.
139157
*
140158
* The first call to this method will create a new Isolate. Subsequent calls will return
141159
* immediately and have no effect.
@@ -149,6 +167,25 @@ FLUTTER_EXPORT
149167
*/
150168
- (BOOL)runWithEntrypoint:(nullable NSString*)entrypoint;
151169

170+
/**
171+
* Runs a Dart program on an Isolate from the main Dart library (i.e. the library that
172+
* contains `main()`).
173+
*
174+
* The first call to this method will create a new Isolate. Subsequent calls will return
175+
* immediately and have no effect.
176+
*
177+
* @param entrypoint The name of a top-level function from the same Dart
178+
* library that contains the app's main() function. If this is FlutterDefaultDartEntrypoint (or
179+
* nil), it will default to `main()`. If it is not the app's main() function, that function must
180+
* be decorated with `@pragma(vm:entry-point)` to ensure the method is not tree-shaken by the Dart
181+
* compiler.
182+
* @param initialRoute The name of the initial Flutter `Navigator` `Route` to load. If this is
183+
* FlutterDefaultInitialRoute (or nil), it will default to the "/" route.
184+
* @return YES if the call succeeds in creating and running a Flutter Engine instance; NO otherwise.
185+
*/
186+
- (BOOL)runWithEntrypoint:(nullable NSString*)entrypoint
187+
initialRoute:(nullable NSString*)initialRoute;
188+
152189
/**
153190
* Runs a Dart program on an Isolate using the specified entrypoint and Dart library,
154191
* which may not be the same as the library containing the Dart program's `main()` function.

shell/platform/darwin/ios/framework/Headers/FlutterViewController.h

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ FLUTTER_EXPORT
5555
*
5656
* The initialized viewcontroller will attach itself to the engine as part of this process.
5757
*
58-
* @param engine The `FlutterEngine` instance to attach to.
58+
* @param engine The `FlutterEngine` instance to attach to. Cannot be nil.
5959
* @param nibName The NIB name to initialize this UIViewController with.
6060
* @param nibBundle The NIB bundle.
6161
*/
@@ -78,6 +78,23 @@ FLUTTER_EXPORT
7878
nibName:(nullable NSString*)nibName
7979
bundle:(nullable NSBundle*)nibBundle NS_DESIGNATED_INITIALIZER;
8080

81+
/**
82+
* Initializes a new FlutterViewController and `FlutterEngine` with the specified
83+
* `FlutterDartProject` and `initialRoute`.
84+
*
85+
* This will implicitly create a new `FlutterEngine` which is retrievable via the `engine` property
86+
* after initialization.
87+
*
88+
* @param project The `FlutterDartProject` to initialize the `FlutterEngine` with.
89+
* @param initialRoute The initial `Navigator` route to load.
90+
* @param nibName The NIB name to initialize this UIViewController with.
91+
* @param nibBundle The NIB bundle.
92+
*/
93+
- (instancetype)initWithProject:(nullable FlutterDartProject*)project
94+
initialRoute:(nullable NSString*)initialRoute
95+
nibName:(nullable NSString*)nibName
96+
bundle:(nullable NSBundle*)nibBundle NS_DESIGNATED_INITIALIZER;
97+
8198
/**
8299
* Initializer that is called from loading a FlutterViewController from a XIB.
83100
*
@@ -117,6 +134,8 @@ FLUTTER_EXPORT
117134
- (NSString*)lookupKeyForAsset:(NSString*)asset fromPackage:(NSString*)package;
118135

119136
/**
137+
* Deprecated API to set initial route.
138+
*
120139
* Attempts to set the first route that the Flutter app shows if the Flutter
121140
* runtime hasn't yet started. The default is "/".
122141
*
@@ -127,9 +146,15 @@ FLUTTER_EXPORT
127146
* Setting this after the Flutter started running has no effect. See `pushRoute`
128147
* and `popRoute` to change the route after Flutter started running.
129148
*
149+
* This is deprecated because it needs to be called at the time of initialization
150+
* and thus should just be in the `initWithProject` initializer. If using
151+
* `initWithEngine`, the initial route should be set on the engine's
152+
* initializer.
153+
*
130154
* @param route The name of the first route to show.
131155
*/
132-
- (void)setInitialRoute:(NSString*)route;
156+
- (void)setInitialRoute:(NSString*)route
157+
FLUTTER_DEPRECATED("Use FlutterViewController initializer to specify initial route");
133158

134159
/**
135160
* Instructs the Flutter Navigator (if any) to go back.
@@ -138,8 +163,7 @@ FLUTTER_EXPORT
138163

139164
/**
140165
* Instructs the Flutter Navigator (if any) to push a route on to the navigation
141-
* stack. The setInitialRoute method should be preferred if this is called before the
142-
* FlutterViewController has come into view.
166+
* stack.
143167
*
144168
* @param route The name of the route to push to the navigation stack.
145169
*/

0 commit comments

Comments
 (0)