简体   繁体   English

如何在 Flutter 中为 Android 设置初始路由?

[英]How do I set the initialRoute for Android in Flutter?

I want to tell to my app to start in a determined route inside the onCreate method of the MainActivity like this:我想告诉我的应用程序从 MainActivity 的 onCreate 方法中的确定路线开始,如下所示:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    GeneratedPluginRegistrant.registerWith(this)
    flutterView.setInitialRoute("anotherRoute") // <-- no result
}

But it gives no results, the dart side stills receive the standard "/" as window.defaultRouteName但它没有给出任何结果,dart 端仍然接收标准的“/”作为window.defaultRouteName

If I set it before calling super, it gives me NullPointer:如果我在调用 super 之前设置它,它会给我 NullPointer:

override fun onCreate(savedInstanceState: Bundle?) {
    flutterView.setInitialRoute("anotherRoute") // <-- NullPointer
    super.onCreate(savedInstanceState)
    GeneratedPluginRegistrant.registerWith(this)
}

You need to override createFlutterView method您需要覆盖createFlutterView方法

  override fun createFlutterView(context: Context): FlutterView {
    val matchParent = WindowManager.LayoutParams(-1, -1)
    val view = FlutterView(this, null as AttributeSet?, this.createFlutterNativeView())
    view.setInitialRoute("/route2")
    view.layoutParams = matchParent

    this.setContentView(view)
    return view
  }

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

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