fix: add listener in ThermionFlutterMethodChannelInterface to unset viewer on dispose

This commit is contained in:
Nick Fisher
2024-10-22 12:48:05 +08:00
parent 45f41f3cf7
commit 5f2407baeb

View File

@@ -14,7 +14,6 @@ import 'package:thermion_flutter_platform_interface/thermion_flutter_texture.dar
/// ///
abstract class ThermionFlutterMethodChannelInterface abstract class ThermionFlutterMethodChannelInterface
extends ThermionFlutterPlatform { extends ThermionFlutterPlatform {
final channel = const MethodChannel("dev.thermion.flutter/event"); final channel = const MethodChannel("dev.thermion.flutter/event");
final _logger = Logger("ThermionFlutterMethodChannelInterface"); final _logger = Logger("ThermionFlutterMethodChannelInterface");
@@ -23,7 +22,7 @@ abstract class ThermionFlutterMethodChannelInterface
Future<ThermionViewer> createViewer({ThermionFlutterOptions? options}) async { Future<ThermionViewer> createViewer({ThermionFlutterOptions? options}) async {
if (viewer != null) { if (viewer != null) {
throw Exception( throw Exception(
"Only one viewer can be created over the lifetime of an application"); "Only one ThermionViewer can be created at any given time; ensure you have called [dispose] on the previous instance before constructing a new instance.");
} }
var resourceLoader = Pointer<Void>.fromAddress( var resourceLoader = Pointer<Void>.fromAddress(
@@ -51,6 +50,10 @@ abstract class ThermionFlutterMethodChannelInterface
uberArchivePath: options?.uberarchivePath); uberArchivePath: options?.uberarchivePath);
await viewer!.initialized; await viewer!.initialized;
viewer!.onDispose(() async {
this.viewer = null;
});
return viewer!; return viewer!;
} }
} }
@@ -60,8 +63,6 @@ abstract class MethodChannelFlutterTexture extends ThermionFlutterTexture {
MethodChannelFlutterTexture(this.channel); MethodChannelFlutterTexture(this.channel);
@override @override
int get flutterId; int get flutterId;
@@ -73,6 +74,4 @@ abstract class MethodChannelFlutterTexture extends ThermionFlutterTexture {
@override @override
int get width; int get width;
} }