?筆者公司的項目最近開始使用了Flutter镐依,由于老項目維護的時間比較長了匹涮。我們采用了原生嵌入一部分Flutter頁面的混合開發(fā)方式。項目開發(fā)完成測試階段我們遇到了一個問題槐壳。就是原生頁面跳轉到FlutterViewController(flutter頁面的根控制器然低,作為flutter的宿主頁面)承載的頁面時候。在返回原生頁面宏粤,重復幾次會導致APP的崩潰脚翘。下圖是官網給的示例
1.png
2.png
// 1.創(chuàng)建引擎
// 2.引擎啟動
// 3.引擎注冊
// 4.創(chuàng)建FlutterViewController
self.flutterEngine = [[FlutterEngine alloc] initWithName:@"my flutter engine"];
// Runs the default Dart entrypoint with a default Flutter route.
[self.flutterEngine run];
// Used to connect plugins (only if you have plugins with iOS platform code).
[GeneratedPluginRegistrant registerWithRegistry:self.flutterEngine];
FlutterViewController *flutterViewController =
[[FlutterViewController alloc] initWithEngine:flutterEngine nibName:nil bundle:nil];
筆者項目也是按照這個文檔步驟來的,后來經過反復的查找绍哎,定位到為題所在,在初始化引擎的方法列表里来农,allowHeadlessExecution
控制gluttervc的生命周期。設置為NO表示崇堰,當FlutterViewController銷毀后沃于,就停掉flutterEngine涩咖。
/**
* Initialize this FlutterEngine with a `FlutterDartProject`.
*
* If the FlutterDartProject is not specified, the FlutterEngine will attempt to locate
* the project in a default location (the flutter_assets folder in the iOS application
* bundle).
*
* A newly initialized engine will not run the `FlutterDartProject` until either
* `-runWithEntrypoint:` or `-runWithEntrypoint:libraryURI:` is called.
*
* @param labelPrefix The label prefix used to identify threads for this instance. Should
* be unique across FlutterEngine instances, and is used in instrumentation to label
* the threads used by this FlutterEngine.
* @param project The `FlutterDartProject` to run.
* @param allowHeadlessExecution Whether or not to allow this instance to continue
* running after passing a nil `FlutterViewController` to `-setViewController:`.
*/
- (instancetype)initWithName:(NSString*)labelPrefix
project:(nullable FlutterDartProject*)project
allowHeadlessExecution:(BOOL)allowHeadlessExecution;