Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run some Javascript file like index.js in Flutter Webview

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');
like image 695
Kashif Ahmed Avatar asked May 22 '19 12:05

Kashif Ahmed


People also ask

Can I use JavaScript library in Flutter?

In Flutter web, the javascript integration is possible using the package:js, mentioned in another answer. Yes.

How do I get Webview response in Flutter?

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.

How to access JS from a flutter web app?

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.

How to call JavaScript functions using flutter webviewcontroller?

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.

How to pass values from flutter to JS using arguments?

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.

How to read content from HTML file in WebView?

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.


1 Answers

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.

like image 140
Yamin Avatar answered Oct 06 '22 14:10

Yamin