Home > Net >  Flutter how I will inject a JavaScript in webview?
Flutter how I will inject a JavaScript in webview?

Time:04-14

In flutter I'm using package webview_flutter: ^3.0.2, here I'm trying to run a javascript in webview and get the result, My effort is like below

body: WebView(
    javascriptMode: JavascriptMode.unrestricted,
    userAgent: 'random',
    initialUrl: 'http://localhost:8000/',
    onWebViewCreated: (controller){
        this.controller = controller;
        print(controller.runJavascriptReturningResult("10   20"));
    },
),

I'm getting result

flutter: Instance of 'Future<String>'

CodePudding user response:

You should await the function's result since it will return a Promise or a Future:

body: WebView(
    javascriptMode: JavascriptMode.unrestricted,
    userAgent: 'random',
    initialUrl: 'http://localhost:8000/',
    onWebViewCreated: (controller) async {
        this.controller = controller;
        String? result = await controller.runJavascriptReturningResult("10   20");
        print(result);
    },
),
  • Related