Skip to content

Commit

Permalink
Throw on invalid platform return type (#62)
Browse files Browse the repository at this point in the history
## Problem (PFL-69)
If the type returned from the platform side is not the expected one, `invokeMethod returns null`. We are handling it by returning default values.

## Changes
As this should never happen, and if it was to, it would be a bug, throw an exception instead of hiding the issue.
  • Loading branch information
krocard authored Oct 5, 2023
1 parent c8d719e commit e2926d1
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions lib/src/player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<T?> _invokeMethod<T>(
Future<T> _invokeMethod<T>(
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<T>(methodName, _buildPayload(data));
final payload = _buildPayload(data);
final result = await _methodChannel.invokeMethod<T>(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
Expand All @@ -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<String>(
Expand Down Expand Up @@ -228,31 +234,28 @@ class Player with PlayerEventHandler implements PlayerApi {

@override
Future<double> get currentTime async =>
await _invokeMethod<double>(Methods.currentTime) ?? 0.0;
_invokeMethod<double>(Methods.currentTime);

@override
Future<double> get duration async =>
await _invokeMethod<double>(Methods.duration) ?? 0.0;
Future<double> get duration async => _invokeMethod<double>(Methods.duration);

@override
Future<double> get timeShift async =>
await _invokeMethod<double>(Methods.getTimeShift) ?? 0.0;
_invokeMethod<double>(Methods.getTimeShift);

@override
Future<void> setTimeShift(double timeShift) async =>
_invokeMethod<void>(Methods.setTimeShift, timeShift);

@override
Future<double> get maxTimeShift async =>
await _invokeMethod<double>(Methods.maxTimeShift) ?? 0.0;
_invokeMethod<double>(Methods.maxTimeShift);

@override
Future<bool> get isLive async =>
await _invokeMethod<bool>(Methods.isLive) ?? false;
Future<bool> get isLive async => _invokeMethod<bool>(Methods.isLive);

@override
Future<bool> get isPlaying async =>
await _invokeMethod<bool>(Methods.isPlaying) ?? false;
Future<bool> get isPlaying async => _invokeMethod<bool>(Methods.isPlaying);

@override
Future<List<SubtitleTrack>> get availableSubtitles async =>
Expand Down

0 comments on commit e2926d1

Please sign in to comment.