From e7c726daeb5af72e194e87d01aedc9046c7f5b25 Mon Sep 17 00:00:00 2001 From: Tommy Chow Date: Sun, 8 Mar 2026 21:40:52 -0700 Subject: [PATCH] guard async callbacks against post-dispose use Co-Authored-By: Claude Opus 4.6 --- lib/screens/channel/video/native_video_store.dart | 8 ++++++++ pubspec.lock | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/screens/channel/video/native_video_store.dart b/lib/screens/channel/video/native_video_store.dart index ae21c771..d724f3bf 100644 --- a/lib/screens/channel/video/native_video_store.dart +++ b/lib/screens/channel/video/native_video_store.dart @@ -44,6 +44,7 @@ abstract class NativeVideoStoreBase with Store implements VideoPlayerInterface { List _qualityObjects = []; var _firstTimeSettingQuality = true; int? _pendingQualityIndex; + var _disposed = false; final _pip = SimplePip(); ReactionDisposer? _disposeAndroidAutoPipReaction; @@ -137,9 +138,12 @@ abstract class NativeVideoStoreBase with Store implements VideoPlayerInterface { } } + if (_disposed) return; + _hlsUrl = twitchGqlApi.buildHlsUrl(login: userLogin, token: token); await _controller!.initialize(); + if (_disposed) return; _pipSub = _controller!.isPipEnabledStream.listen((isPip) { runInAction(() { @@ -180,6 +184,7 @@ abstract class NativeVideoStoreBase with Store implements VideoPlayerInterface { _pendingQualityIndex = 1; } else { SharedPreferences.getInstance().then((prefs) { + if (_disposed) return; final lastQuality = prefs.getString('last_stream_quality'); if (lastQuality != null) { final index = _availableStreamQualities.indexOf(lastQuality); @@ -331,6 +336,7 @@ abstract class NativeVideoStoreBase with Store implements VideoPlayerInterface { @action Future handleRefresh() async { HapticFeedback.lightImpact(); + _disposed = false; _loading = true; _paused = true; _firstTimeSettingQuality = true; @@ -464,6 +470,8 @@ abstract class NativeVideoStoreBase with Store implements VideoPlayerInterface { @override @action void dispose() { + _disposed = true; + if (Platform.isAndroid) { SimplePip.isAutoPipAvailable.then((isAutoPipAvailable) { if (isAutoPipAvailable) _pip.setAutoPipMode(autoEnter: false); diff --git a/pubspec.lock b/pubspec.lock index d4a5fb57..a7970c94 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -102,7 +102,7 @@ packages: description: path: "." ref: frosty-patches - resolved-ref: cc173d8329f79f12960dd7280ef2709496ff0ef0 + resolved-ref: "6795a21905584cd8a04fee2834175d84e70e8ab6" url: "https://github.com/tommyxchow/flutter-native-video-player.git" source: git version: "0.4.11"