I am new to flutter. I have developed js to flutter dart communication using webview_flutter. like this :
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example'),
actions: <Widget>[
MenuList(_controller.future),
],
),
body: Builder(builder: (BuildContext context) {
return WebView(
initialUrl: localServerUrl,
javascriptMode: JavascriptMode.unrestricted,
onWebViewCreated: (WebViewController webViewController) {
_controller.complete(webViewController);
},
javascriptChannels: <JavascriptChannel>[
_scanBarcode(context),
].toSet(),
onPageFinished: (String url) {
//TODO : events after page loading finished
},
);
}),
),
);
}
JavascriptChannel _scanBarcode(BuildContext context) {
return JavascriptChannel(
name: 'Barcode',
onMessageReceived: (JavascriptMessage message) {
String result = scanBarcode(context);
******I got result of scanned barcode in result variable******
});
}
server html file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="script.js"></script>
</head>
<body>
<button id="btnBarcode" style="margin-top:20px; height:30px;">Scan Barcode</button>
</body>
</html>
and this is JS file
function scanBarcode(message) {
if (window.Barcode && window.Barcode.postMessage) {
Barcode.postMessage(message);
}
}
window.onload = function () {
document.getElementById('btnBarcode').onclick = function () {
scanBarcode("Scan Barcode");
}
}
I have successfully got result of scanned barcode in javascript channel _scanBarcode in Dart file.
Now I want to callback this barcode result back to JS file in scanBarcode function.
I have researched so much time but not getting anything.
I am stuck here. Can anyone help me?? Thank you so much in advance.