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

[BUG] Exception when receive messages from javascript #975

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,8 @@ class WebYoutubePlayerIframeController extends PlatformWebViewController {
final completer = Completer<String>();
final subscription = window.onMessage.listen(
(event) {
final data = jsonDecode(event.data.dartify() as String);

if (data is Map && data.containsKey(key)) {
final data = handleJsMessageToMap(event.data.dartify());
if (data.containsKey(key)) {
completer.complete(data[key].toString());
}
},
Expand Down Expand Up @@ -206,9 +205,9 @@ class YoutubePlayerIframeWeb extends PlatformWebViewWidget {
if (channelParams != null) {
window.onMessage.listen(
(event) {
channelParams.onMessageReceived(
JavaScriptMessage(message: event.data.dartify() as String),
);
channelParams.onMessageReceived(JavaScriptMessage(
message: handleJsMessageToString(event.data.dartify()),
));
},
);
}
Expand All @@ -217,6 +216,26 @@ class YoutubePlayerIframeWeb extends PlatformWebViewWidget {
}
}

Map handleJsMessageToMap(Object? jsMessage) {
return switch (jsMessage) {
String message => jsonDecode(message),
Map map => map,
Object? data => {
'error':
'[$YoutubePlayerIframeWeb] Invalid message type "${data.runtimeType}": $data'
},
};
}

String handleJsMessageToString(Object? jsMessage) {
return switch (jsMessage) {
String message => message,
Map map => jsonEncode(map),
Copy link
Owner

Choose a reason for hiding this comment

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

if it's already a map, do we need to encode and decode ?

Copy link
Author

@davidsdearaujo davidsdearaujo Sep 26, 2024

Choose a reason for hiding this comment

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

This is not always a map... I'm encoding at this moment to make as less changes as possible in the existing logic

Copy link
Author

@davidsdearaujo davidsdearaujo Sep 28, 2024

Choose a reason for hiding this comment

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

I've separated this logic into two distinct handlers in this commit: Improve web controller's JS message deserialization

Object? data =>
'[$YoutubePlayerIframeWeb] Invalid message type "${data.runtimeType}": $data',
};
}

extension type YoutubeIframeElement._(HTMLIFrameElement element) {
/// A class that represents a YouTube iframe element.
YoutubeIframeElement({required int id})
Expand Down