Solution 1 :

Changing Survey.postMessage() to window.Survey.postMessage() may work. Im not sure how to use evalJavascipt with WebviewScaffold, i’m using like this:

final _webView = new FlutterWebviewPlugin();
final Set<JavascriptChannel> jsChannels = [
  JavascriptChannel(
      name: 'Survey',
      onMessageReceived: (JavascriptMessage message) {
        print(message.message);
      }),
].toSet();
_webView.launch(url, jsChannels);
_webView.onStateChanged.listen((event) {
  if (event.type == WebViewState.finishLoad) {
    _webView.evalJavascript('Your Js Code' + 
    'window.Survey.postMessage("Your Return Mes.")');
  }
});

Problem :

I am using a WebView to load a local HTML file that contains the callback –

function onReward(data){
console.log("onReward: " + data.earnedThisSession);
Survey.postMessage(data.earnedThisSession);
}

This callback gets triggered when a user completes an action, I am currently facing two problems

  1. The data.earnedThisSession returned by the function differs from every user and I want to get this as a variable in my dart code to reward my user
  2. My WebView dependency is not printing console messages in my debug console

This is my JavascriptChannel

final Set<JavascriptChannel> jsChannels = [
  JavascriptChannel(
      name: 'Survey',
      onMessageReceived: (JavascriptMessage message) {
        print(message.message);
      }),
].toSet();

This is my WebviewScaffold

FutureBuilder<String>(
              future: _loadLocalHTML(),
              builder: (context, snapshot) {
                if (snapshot.hasData) {
                  return Scaffold(
                    appBar: AppBar(),
                    body: WebView(
                      initialUrl: Uri.dataFromString(
                        snapshot.data,
                        mimeType: 'text/html',
                        encoding: Encoding.getByName('utf-8'),
                      ).toString(),
                      javascriptChannels: jsChannels,
                      javascriptMode: JavascriptMode.unrestricted,
                    ),
                  );
                }

How do I use evalJavascript in this to fetch my reward data?

Comments

Comment posted by Darlan Dieterich

you are using stateful widget?

Comment posted by Arnav

Yes @DarlanDieterich

Comment posted by bl4ckr0se

@DarlanD. I don’t undestand why you are writing comments without the context of the response. wtf it means try define a differente initialUrl for this answer?

By