diff --git a/lib/src/player.dart b/lib/src/player.dart index 155e2986..1cd59d76 100644 --- a/lib/src/player.dart +++ b/lib/src/player.dart @@ -123,15 +123,21 @@ class Player with PlayerEventHandler implements PlayerApi { // Can be used to call methods on the platform side that return a single // primitive value that is natively supported by the method channel. - Future _invokeMethod( + Future _invokeMethod( String methodName, [ dynamic data, ]) async { - final result = await _initializationResult; - if (!result) { - return Future.error('Error initializing player on native platform side.'); + final initSuccess = await _initializationResult; + if (!initSuccess) { + throw Exception('Error initializing player on native platform side.'); } - return _methodChannel.invokeMethod(methodName, _buildPayload(data)); + final payload = _buildPayload(data); + final result = await _methodChannel.invokeMethod(methodName, payload); + 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.'); + } + return result; } // Can be used to call methods on the platform side that return a complex @@ -143,7 +149,7 @@ class Player with PlayerEventHandler implements PlayerApi { ]) async { final result = await _initializationResult; if (!result) { - return Future.error('Error initializing player on native platform side.'); + throw Exception('Error initializing player on native platform side.'); } final jsonString = await _methodChannel.invokeMethod( @@ -228,15 +234,14 @@ class Player with PlayerEventHandler implements PlayerApi { @override Future get currentTime async => - await _invokeMethod(Methods.currentTime) ?? 0.0; + _invokeMethod(Methods.currentTime); @override - Future get duration async => - await _invokeMethod(Methods.duration) ?? 0.0; + Future get duration async => _invokeMethod(Methods.duration); @override Future get timeShift async => - await _invokeMethod(Methods.getTimeShift) ?? 0.0; + _invokeMethod(Methods.getTimeShift); @override Future setTimeShift(double timeShift) async => @@ -244,15 +249,13 @@ class Player with PlayerEventHandler implements PlayerApi { @override Future get maxTimeShift async => - await _invokeMethod(Methods.maxTimeShift) ?? 0.0; + _invokeMethod(Methods.maxTimeShift); @override - Future get isLive async => - await _invokeMethod(Methods.isLive) ?? false; + Future get isLive async => _invokeMethod(Methods.isLive); @override - Future get isPlaying async => - await _invokeMethod(Methods.isPlaying) ?? false; + Future get isPlaying async => _invokeMethod(Methods.isPlaying); @override Future> get availableSubtitles async =>