简体   繁体   English

如何让 setInitialRoute 在不同的视图上启动我的 Flutter iOS 应用程序?

[英]How to get setInitialRoute to start my Flutter iOS app on different views?

In flutter I have this:在颤振中,我有这个:

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      routes: <String, WidgetBuilder> {
        '/Cards': (BuildContext context) => new PageSelectorDemo(),
      },
      onGenerateRoute: (RouteSettings settings) => new MaterialPageRoute(
        builder: (BuildContext ctx) => new PageSelectorDemo(),
      ),
      theme: new ThemeData(
        primarySwatch: Colors.deepPurple,
      ),
      home: new MyHomePage(title: 'Flutter View'),
    );
  }
}

In my iOS code I have this:在我的 iOS 代码中,我有这个:

func getFlutterController() -> UIViewController?
{
    self.flutterController = FlutterViewController(project: nil, nibName: nil, bundle: nil)

    if let controller = self.flutterController
    {
        controller.setInitialRoute("/Cards") // setting the route
        self.messageChannel = FlutterBasicMessageChannel.init(name: "channel", binaryMessenger: controller, codec: FlutterStandardMessageCodec.sharedInstance())
        if let channel = self.messageChannel
        {
            channel.setMessageHandler() { (message:Any?, reply:FlutterReply) in
                ViewController.counter += 1
                self.labelMessage.text = "message recieved: \(ViewController.counter)"
            }
        }
    }
    return self.flutterController
}

Why doesn't this work?为什么这不起作用? The flutter code just loads the initial view and not my view from the "/Cards" route. flutter 代码只加载初始视图,而不是我从“/Cards”路由的视图。

我在从 iOS 设置初始路由时也遇到了问题,看起来它可能是 flutter 中的一个错误: https : //github.com/flutter/flutter/issues/27216

I found problem is self.flutterEngine?.run(withEntrypoint: nil) in document Add Flutter to existing apps .我发现问题是self.flutterEngine?.run(withEntrypoint: nil)在文档Add Flutter to existing apps If you do like that, FlutterEngine will run before you create FlutterViewController so you cannot setInitialRoute .如果您这样做, FlutterEngine将在您创建FlutterViewController之前运行,因此您无法setInitialRoute To solve, you must remove that line in AppDelegate , init FlutterViewController without FlutterEngine let flutterViewController = FlutterViewController(nibName: nil, bundle: nil) , and setInitialRoute , final call flutterEngine?.run(withEntrypoint: nil) .要解决,你必须删除在该行AppDelegate ,初始化FlutterViewController没有FlutterEngine let flutterViewController = FlutterViewController(nibName: nil, bundle: nil) ,以及setInitialRoute ,最后调用flutterEngine?.run(withEntrypoint: nil)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM