From 882fd51ffe9cf8c97bf57da0f13ba683b60e19f1 Mon Sep 17 00:00:00 2001 From: Mario Graf Date: Tue, 26 Nov 2024 18:01:18 +0100 Subject: [PATCH 1/2] Document race condition and produce meaningful error instead of causing an exception --- .../player_view_platform_method_channel.dart | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/src/platform/player_view_platform_method_channel.dart b/lib/src/platform/player_view_platform_method_channel.dart index 2cade432..82921650 100644 --- a/lib/src/platform/player_view_platform_method_channel.dart +++ b/lib/src/platform/player_view_platform_method_channel.dart @@ -26,7 +26,7 @@ class PlayerViewPlatformMethodChannel extends PlayerViewPlatformInterface { final void Function() _handleExitFullscreen; final void Function()? _onViewCreated; final EventDeserializer _eventDeserializer = EventDeserializer(); - late final MethodChannel _methodChannel; + MethodChannel? _methodChannel; late final EventChannel _eventChannel; @override @@ -77,7 +77,13 @@ class PlayerViewPlatformMethodChannel extends PlayerViewPlatformInterface { String methodName, [ dynamic data, ]) async { - final result = await _methodChannel.invokeMethod(methodName, data); + if (_methodChannel == null) { + return Future.error( + 'Method channel for player view not initialized yet.', + ); + } + + final result = await _methodChannel?.invokeMethod(methodName, data); if (result is! T) { // result is T?, if it `is` not T => T is not nullable and result is null. throw Exception('Native $methodName returned null.'); @@ -85,6 +91,10 @@ class PlayerViewPlatformMethodChannel extends PlayerViewPlatformInterface { return result; } + // TODO(mario): Method channels are created only once `_onPlatformViewCreated` + // is called. Calls to `_invokeMethod` that happen before that lead to an + // error. This race condition can be fixed the same way as for + // PlayerPlatformMethodChannel` by using an `_initializationResult`. void _onPlatformViewCreated(int id) { _methodChannel = ChannelManager.registerMethodChannel( name: '${Channels.playerView}-$id', From 1cf497c1dc3684adeb5adea2f431438b1951ce25 Mon Sep 17 00:00:00 2001 From: Mario Graf Date: Tue, 26 Nov 2024 18:05:55 +0100 Subject: [PATCH 2/2] Update doc --- lib/src/platform/player_view_platform_method_channel.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/platform/player_view_platform_method_channel.dart b/lib/src/platform/player_view_platform_method_channel.dart index 82921650..79ba0bdc 100644 --- a/lib/src/platform/player_view_platform_method_channel.dart +++ b/lib/src/platform/player_view_platform_method_channel.dart @@ -94,7 +94,7 @@ class PlayerViewPlatformMethodChannel extends PlayerViewPlatformInterface { // TODO(mario): Method channels are created only once `_onPlatformViewCreated` // is called. Calls to `_invokeMethod` that happen before that lead to an // error. This race condition can be fixed the same way as for - // PlayerPlatformMethodChannel` by using an `_initializationResult`. + // `PlayerPlatformMethodChannel` by using an `_initializationResult`. void _onPlatformViewCreated(int id) { _methodChannel = ChannelManager.registerMethodChannel( name: '${Channels.playerView}-$id',