Skip to content

Use initWithEngine:nibName:bundle: method to create FlutterViewController, and then setInitialRoute not working #27216

@JsouLiang

Description

@JsouLiang

When I use initWithEngine:nibName:bundle: to create FlutterViewController, and then setInitialRoute: ;I find it doesn't work; When I use window.defaultRouteName in main.dart I get '/'
This is the code:

// Appdelegate

    FlutterEngine *engine = [[FlutterEngine alloc] initWithName:@"io.flutter" project:nil allowHeadlessExecution:YES];
    self.engine = engine;
    [self.engine runWithEntrypoint:nil];

// ViewController

    FlutterEngine *flutterEngine = [(AppDelegate *)[[UIApplication sharedApplication] delegate] engine];

    FlutterViewController* flutterViewController = [[FlutterViewController alloc] initWithEngine:flutterEngine nibName:nil bundle:nil];

    [flutterViewController setInitialRoute:@"myApp"];
// main.dart

    void main() => runApp(_widgetForRoute(window.defaultRouteName));

    Widget _widgetForRoute(String route) {
      switch (route) {
        case 'myApp':
          return new MyApp();
        case 'home':
          return new HomePage();
        default:
          return Center(
            child: Text('Unknown route: $route', textDirection: TextDirection.ltr),
          );
      }
    }

I always get a Text and content is ''Unknown route: /'

Metadata

Metadata

Assignees

No one assigned

    Labels

    a: existing-appsIntegration with existing apps via the add-to-app flowengineflutter/engine related. See also e: labels.f: routesNavigator, Router, and related APIs.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions