How to set Initial Route from native iOS

1,128

This issue is now fixed and as of Flutter 1.22 it can be done using:

While initializing flutter engine:

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

and, Directly while creating the FlutterViewController,

 let flutterViewController = FlutterViewController(
    project: nil,
    initialRoute: "/onboarding",
    nibName: nil,
    bundle: nil)
Share:
1,128
user3592917
Author by

user3592917

Updated on December 19, 2022

Comments

  • user3592917
    user3592917 over 1 year

    I'm integrate Flutter module to ios Native project, I want to set Initial Route from ios native, but it not work, it use default route.

    ViewController.swift

    import UIKit
    import Flutter
    
    class ViewController: UIViewController {
    
        let flutterEngine = FlutterEngine(name: "test")
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view.
            flutterEngine.navigationChannel.invokeMethod("setInitialRoute", arguments:"/home")
            flutterEngine.run();
    
        }
    
        @IBAction func handleClick(_ sender: Any) {
            let flutterViewController = FlutterViewController(engine: flutterEngine, nibName: nil, bundle: nil)
            flutterViewController.setInitialRoute("/home")
            self.navigationController?.pushViewController(flutterViewController, animated: true)
        }
    
    }
    

    main.dart

    import 'package:flutter/material.dart';
    import 'package:flutter/services.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      final _route = <String, WidgetBuilder>{
        "/login": (context) => Login(),
        "/home": (context) => Home()
      };
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          routes: _route,
          title: "App", // Title ของหน้า
          home: Scaffold(
            // หน้าจอหลัก
            appBar: AppBar(
              title: Text("App Navi"),
            ),
            body: Login(),
          ),
        );
      }
    }
    

    Login, Home file please see in image, beacause stackoverflow can't to post text "It looks like your post is mostly code; please add some more details."

    enter image description here

    enter image description here

    enter image description here

    enter image description here