FlutterEngine使用不當導致App崩潰

?筆者公司的項目最近開始使用了Flutter,由于老項目維護的時間比較長了。我們采用了原生嵌入一部分Flutter頁面的混合開發(fā)方式。項目開發(fā)完成測試階段我們遇到了一個問題。就是原生頁面跳轉到FlutterViewController(flutter頁面的根控制器,作為flutter的宿主頁面)承載的頁面時候。在返回原生頁面,重復幾次會導致APP的崩潰。下圖是官網(wǎng)給的示例


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];

筆者項目也是按照這個文檔步驟來的,后來經(jīng)過反復的查找,定位到為題所在,在初始化引擎的方法列表里,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;
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容