Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Initial Route from native iOS

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

like image 848
user3592917 Avatar asked Nov 07 '22 08:11

user3592917


1 Answers

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)
like image 130
Let's_Create Avatar answered Nov 14 '22 22:11

Let's_Create