dispose static method

Future<void> dispose()

反初始化引擎,清空所有事件 / 查询处理器,并释放单例。

Implementation

static Future<void> dispose() async {
  final inst = _shared;
  if (inst == null) return;
  if (!inst._inited) {
    _shared = null;
    return;
  }
  try {
    // 先注销事件端口,避免 Uninit 期间引擎仍向已关闭的端口投递(port-based
    // 下投递失败会被丢弃,不再存在调用已删除 NativeCallable 的风险)。
    if (inst._adapterInstalled) {
      inst._ffi.shutdownEventChannel();
    }
    inst._ffi.uninit(inst._handle);
    inst._ffi.destroy(inst._handle);
  } finally {
    // 关闭 call 响应通道:以错误完成未决 completer、注销 send_port、关端口。
    inst._ffi.shutdownCallChannel();
    inst._eventHandlers.clear();
    inst._queryHandlers.clear();
    inst._adapterInstalled = false;
    inst._handle = nullptr;
    inst._inited = false;
    _shared = null;
  }
  developer.log('EngineBridge disposed', name: 'EngineBridge');
}