title: How to use Flutter webview evaluateJavascript?
body: <p>I try to use <code>webview evaluateJavascript</code> and got error. <code>WebView Controller</code> has 2 function; <code>evaluateJavascript</code>, and <code>loadUrl</code>. I tested both in <code>onPageFinished: (url){}</code>, so I can initialise my javascript code. Documentation is not clear. But the error tells me something missing pr not implemented in webview plugin. I am trying to load page and initialising some basic javascript so I can hide some part of the page.</p>
  
  <p>How to use Flutter webview evaluateJavascript?</p>
  
  <p><strong>Error:</strong></p>
  
  <pre><code>Syncing files to device iPhone X...
  flutter: Page finished loading: https://stackoverflow.com/
  [VERBOSE-2:ui_dart_state.cc(148)] Unhandled Exception: PlatformException(evaluateJavaScript_failed, Failed evaluating JavaScript, JavaScript string was: 'javascript:(function() { var head = document.getElementsByClassName('top-bar js-top-bar top-bar__network _fixed')[0].style.display='none'; })()'
  Error Domain=WKErrorDomain Code=4 "A JavaScript exception occurred" UserInfo={WKJavaScriptExceptionLineNumber=1, WKJavaScriptExceptionMessage=TypeError: undefined is not an object (evaluating 'document.getElementsByClassName('top-bar js-top-bar top-bar__network _fixed')[0].style'), WKJavaScriptExceptionColumnNumber=117, WKJavaScriptExceptionSourceURL=https://stackoverflow.com/, NSLocalizedDescription=A JavaScript exception occurred})
  #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:564:7)
  #1      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:302:33)
  &lt;asynchronous suspension&gt;
  #2      WebViewController.evaluateJavascript (package:web&lt;…&gt;
  Application finished.
  </code></pre>
  
  <p><strong>Code:</strong></p>
  
  <pre><code>    class _WebViewExampleState extends State&lt;WebViewExample&gt; {
      WebViewController _myController;
        bool _loadedPage = false;
  
        @override
        void initState() {
          // TODO: implement initState
          super.initState();
          setState(() {
            _loadedPage = false;
          });
        }
  
  
        @override
        Widget build(BuildContext context) {
          return Scaffold(
            appBar: AppBar(
              backgroundColor: Colors.green,
              title: const Text(
                ’Stackoverflow’,
                style: TextStyle(fontSize: 14),
              ),
  
            ),
            body: Builder(builder: (BuildContext context) {
              return new Stack(
                children: &lt;Widget&gt;[
                  new WebView(
                    initialUrl: 'https://stackoverflow.com',
                    javascriptMode: JavascriptMode.unrestricted,
                    onWebViewCreated: (controller){
                      _myController = controller;
                    },
                    javascriptChannels: &lt;JavascriptChannel&gt;[
                      _toasterJavascriptChannel(context),
                    ].toSet(),
                    onPageFinished: (url){
                      print('Page finished loading: $url');
  
                      _myController.evaluateJavascript("javascript:(function() { " +
                          "var head = document.getElementsByClassName('top-bar js-top-bar top-bar__network _fixed')[0].style.display='none'; " +
                          "})()");
  
  /*
                  _myController.loadUrl("javascript:(function() { " +
                      "var head = document.getElementsByClassName('top-bar js-top-bar top-bar__network _fixed')[0].style.display='none'; " +
                      "})()");
  */
  
                      setState(() {
                        _loadedPage = true;
                      });
                    },
                  ),
                  _loadedPage == false
                      ? new Center(
                          child: new CircularProgressIndicator(
                              backgroundColor: Colors.green),
                        )
                      : new Container(),
                ],
              );
            }),
  
          );
        }
  
        JavascriptChannel _toasterJavascriptChannel(BuildContext context) {
          return JavascriptChannel(
              name: 'Toaster',
              onMessageReceived: (JavascriptMessage message) {
                Scaffold.of(context).showSnackBar(
                  SnackBar(content: Text(message.message)),
                );
              });
        }
  
      }
  </code></pre>