I am using webview_flutter_plus with flutter and using local files from assets folder. Page is loading fine but sometimes (every now and then), I keep getting below error in console even though I have setUserData() function setup in js file. Surprisingly sometimes it works and prints through js file.
I/chromium( 7190): [INFO:CONSOLE(1)] "Uncaught ReferenceError: setUserData is not defined", source: http://localhost:42369/assets/html/home.html (1)
Function in js file:
function setUserData(json) {
console.log(json);
}
First I load data from SQFLite and display on webpage. After that if device is online, app downloads data from online server and updates SQFLite. Below is the dart file:
late WebViewPlusController _webViewPlusController;
Timer? timer;
bool _webViewInitialized = false;
void _checkIfWebViewInitialized(){
timer = Timer.periodic(const Duration(seconds: 1), (Timer t) {
if(_webViewInitialized){
timer?.cancel();
_getSqliteDetails();
}
});
}
Future<void> _getSqliteDetails() async {
final userDetails = Map.of(await UserProfileCrud.getUserDetailsByDisplayId(1917));
if(userDetails.isNotEmpty) {
userDetails['status'] = 'Success';
final encodedUserData = convert.jsonEncode(userDetails);
_webViewPlusController.webViewController.runJavascript("setUserData('$encodedUserData')");
}
// Check Online Server Data
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: WebViewPlus(
javascriptMode: JavascriptMode.unrestricted,
initialUrl: 'assets/html/home.html',
onWebViewCreated: (controller){
_webViewPlusController = controller;
//CookieManager().clearCookies();
setState((){
_webViewInitialized = true;
});
},
),
);
}
Is there any other way to it that I am unaware of? Kindly help as new to flutter.
Thanks in advance.