问题:在自定义FlutterFragment的configureFlutterEngine方法中初始化页面路由发现一直是'/'
原因:当 configureFlutterEngine
方法被调用时,Flutter 已经完成了初始化并设置了默认的初始路由(通常是 '/'
)。
在 Android 项目中,FlutterFragment
在 onAttach
方法中创建 FlutterEngine
。在 FlutterFragment
的生命周期中,onAttach
方法早于 configureFlutterEngine
方法。也就是说,在 configureFlutterEngine
被调用到之前,FlutterEngine
已经初始化完成并且已经解析了默认的初始路由。
因此,你在 configureFlutterEngine
中设置初始路由,这是在 FlutterEngine
解析默认初始路由之后的事情,这也解释了为什么你的设置没有生效。
也就是说,如果要在的 configureFlutterEngine
方法中设置初始路由,你需要创建自己的 FlutterFragment
并覆盖 provideFlutterEngine
方法。在这个方法中,先创建一个 FlutterEngine
,设置好初始路由,然后再返回它。
class CustomFlutterFragment : FlutterFragment() { override fun provideFlutterEngine(context: Context): FlutterEngine { val flutterEngine = FlutterEngine(context) flutterEngine.navigationChannel.setInitialRoute("/shoppingCart") flutterEngine.dartExecutor.executeDartEntrypoint( DartExecutor.DartEntrypoint.createDefault() ) return flutterEngine } override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) { super.configureFlutterEngine(flutterEngine) //GeneratedPluginRegistrant.registerWith(flutterEngine) Log.d("flutter", "CustomFlutterFragment configureFlutterEngine: ") } }
标签:初始化,FlutterFragment,自定义,flutterEngine,configureFlutterEngine,FlutterEngine,路由 From: https://www.cnblogs.com/baiyi168/p/18230302