Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw on invalid platform return type #62

Merged
merged 6 commits into from
Oct 5, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions lib/src/player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,28 @@ 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?> _invokeMethodNullable<T>(
String methodName, [
dynamic data,
]) 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.');
}
return _methodChannel.invokeMethod<T>(methodName, _buildPayload(data));
}

Future<T> _invokeMethod<T>(
String methodName, [
dynamic data,
]) async {
final result = await _invokeMethodNullable<T>(methodName, data);
if (result == null) {
throw Exception('Invalid type returned from the native platform side.');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also print the method name for better debugging:

Suggested change
throw Exception('Invalid type returned from the native platform side.');
throw Exception('Invalid type returned for $methodName from the native platform side.');

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}
return result;
}

// Can be used to call methods on the platform side that return a complex
// object that is not natively supported by the method channel.
Future<T?> _invokeObjectMethod<T>(
Expand All @@ -143,7 +154,7 @@ class Player with PlayerEventHandler implements PlayerApi {
]) async {
final result = await _initializationResult;
if (!result) {
return Future.error('Error initializing player on native platform side.');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrapping in Future.error is unnecessary in an async function, it is equivalent to throw.
Additionally, the code was throwing a string when Dart recommends to only throw subclasses of Exception or Error.

throw Exception('Error initializing player on native platform side.');
}

final jsonString = await _methodChannel.invokeMethod<String>(
Expand Down Expand Up @@ -228,31 +239,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