web_flutter_connect
v1.0.3
Published
For Web-Flutter connection
Downloads
16
Maintainers
Readme
Web-Flutter connect package
Flutter
- Call Flutter
- await Flutter.call(hanlderName: string, ...params: any[])
- Send message to Flutter
- await Flutter.send(data: any) --> await Flutter.call("onMessage", data)
- In Flutter code:
///Listen message from web
this.controller?.addJavaScriptHandler(
handlerName: WebviewModuleKeys.onMessage,
callback: (List<dynamic> params) async {
if (widget.onMessage != null) {
widget.onMessage!(params[0]);
}
});
- Close webview
- await Flutter.close() --> await Flutter.call("closeWebview", data)
- Notify Web content did load
- await Flutter.setDidLoad() --> await Flutter.call("onContentDidLoad", data)
- Print to Flutter console
- await Flutter.print(data) --> await Flutter.call("onWebViewPrint", data)
- In Flutter code:
///Listen print from web
this.controller?.addJavaScriptHandler(
handlerName: WebviewModuleKeys.onPrint,
callback: (List<dynamic> params) async {
print("*** PRINT FROM WEBVIEW: ***");
if (params.isNotEmpty) {
print("${params.first}");
}
print("*** END PRINT FROM WEBVIEW ***");
});
WebView
- WebView.listenFromFlutter: Listen message from Flutter
const subscription = WebView.listenFromFlutter((message: any) => {
Flutter.print({
"title": "Receive message from Flutter",
"data": message,
});
})
....
//Unsubscribe
subscription.unsubscribe();
- WebView.send(message: any)
- Call in Flutter to send a message to WEB
try {
final strMessage = jsonEncode(message);
this.controller?.evaluateJavascript(source: """
window.WebView.send('$strMessage');
""");
} catch (e) {
throw {
"error": "Cannot send unencodable message to WebView",
"message": message,
};
}