Open vikasgupta4190 opened 4 years ago
Resolved. Thnx
@vikasgupta4190 did you resolve this?
How did you write in flutter code?
@vikasgupta4190 How about a Widget?
return WebviewScaffold(
url: url,
javascriptChannels: <JavascriptChannel>{
JavascriptChannel(
name: 'Android',
onMessageReceived: (message) {
print(message);
},
),
},
);
flutterWebViewPlugin.onStateChanged.listen((state) async {
if(state.type == WebViewState.finishLoad) {
String script = 'window.addEventListener("message", sendWhatsApp,false);' +
'function sendWhatsApp(msg) {Android.postMessage("data");}';
flutterWebViewPlugin.evalJavascript(script);
}
It don't work. for addJavascriptInterface
Here is my code. MainActivity.kt override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) { GeneratedPluginRegistrant.registerWith(flutterEngine); }
WebviewManager.java webView.addJavascriptInterface(new WebAppInterface(), "Android"); ////////////// public class WebAppInterface { @JavascriptInterface public void getPostMessage(String value){ Map<String, Object> postMessageMap = new HashMap<>(); postMessageMap.put("postMessage", value); FlutterWebviewPlugin.channel.invokeMethod("onPostMessage", postMessageMap); } }
dart file : flutterWebViewPlugin.onStateChanged.listen((state) async {