how I can run Javascript file in the flutter_webview_plugin. I try it with this.
flutterWebViewPlugin.evalJavascript("require('./index.js');");
But nothing happens.
when I try to run flutter code it's shows nothing
my index.Js
file contains a simple alert statement
alert('hello world');
In Flutter web, the javascript integration is possible using the package:js, mentioned in another answer. Yes.
First, create an object of WebViewController and Completer class, here Completer class is used for a future task. Then handle the callback of completer inside build method and then . complete method is to be called on the completer after the webview is created.
The following snippet demonstrates how access JS from a Flutter web app. Dart.js is a built-in library that can interop with JavaScript. Create a JS file in the web directory and add some functions to it. It calls the function from the global Window execution context. You can define a top level function, or define values directly on window.
The webViewController instance is also used to call javascript functions using evaluateJavascript method. Here we are calling the ‘ add ’ function in the local html file which adds two numbers that are sent from Flutter and show in a p tag in the Webview. Please share and comment your valuable thoughts.
import 'dart:js' as js; js.context.callMethod('alertMessage', ['Flutter is calling upon JavaScript!']); You can now pass values from Flutter to JS by including them as arguments.
This class containf loadString () method which will takes path of html file. String fileHtmlContents = await rootBundle.loadString ('assets/demo.html'); After read the content from html file we need to pass this data to webview, this is done by webviewcontroller.
First, You used "require" function. this function is not implemented in javascript itself. it's a part of NodeJs. so that function will not work.
In order to load a js file into flutter, you should consider it as a text file and load it properly. So, you need to add the file to assets folder, add into pubspec file, then load it. read the full answer here
Second, you used evalJavascript. this function can be used in many different situations. but it will work only if you have a view panel.
Check below example:
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';
main() async {
String jsCode = await rootBundle.loadString('assets/javascript.js');
runApp(new MaterialApp(
home: LunchWebView(jsCode),
));
}
class LunchWebView extends StatelessWidget {
final String text;
LunchWebView(this.text);
@override
Widget build(BuildContext context) {
final FlutterWebviewPlugin flutterWebviewPlugin = FlutterWebviewPlugin();
flutterWebviewPlugin.launch('https://www.google.com');
flutterWebviewPlugin.evalJavascript(text);
return Container();
}
}
NOTE: : I didn't handle reloading and other exceptions. you should check if there is any webview object open and then call Lunch method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With