笔者公司的项目最近开始使用了Flutter,由于老项目维护的时间比较长了。我们采用了原生嵌入一部分Flutter页面的混合开发方式。项目开发完成测试阶段我们遇到了一个问题。就是原生页面跳转到FlutterViewController(flutter页面的根控制器,作为flutter的宿主页面)承载的页面时候。在返回原生页面,重复几次会导致APP的崩溃。下图是官网给的示例
1.png
2.png
// 1.创建引擎
// 2.引擎启动
// 3.引擎注册
// 4.创建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;
网友评论