Search code examples
flutterwebviewreload

Null check operator used on a null value Error in reload webview


Please I'm working on a flutter WebView project and I need to reload my page if I click a button but it gave me this Error : Null check operator used on a null value. Thank you in advance

this is my code :

WebView(
                  initialUrl: "https://wikoget.com",
                  javascriptMode: JavascriptMode.unrestricted,
                  onWebViewCreated: (WebViewController webViewController) {
                    _webViewController = webViewController;
                    _controller.complete(webViewController);
                  },
                  onPageFinished: (String url) {
                    _webViewController
                        .evaluateJavascript("javascript:(function() { " +
                        "var head = document.getElementsByClassName('main-header-bar-wrap')[0];" +
                        "head.parentNode.style.cssText = ' position: sticky;position: -webkit-sticky; top : 0 ';" +
                        "var footer = document.getElementsByTagName('footer')[0];" +
                        "footer.parentNode.removeChild(footer);" +
                        "})()")
                        .then((value) => debugPrint('Page finished loading Javascript'));
                  },
                  onWebResourceError: (error) => setState(() {
                    isError = true;
                  }),
                ),
              if (isError)Center(

                child :RaisedButton(
                        padding: const EdgeInsets.symmetric(vertical: 10,horizontal: 30),
                        onPressed: ()=>  _webViewController.reload(),

                        color: Color(int.parse("0xff135888")),
                        shape:const RoundedRectangleBorder(
                          borderRadius: BorderRadius.all(Radius.circular(30)),
                        ),
                        child:const Text("Réessayez",style: TextStyle(color:Colors.white),)
                    ),
                  ],
                ),
              ),

Solution

  • Although your code is incomplete, here's what you need to make your webview reload easily.

    Create a webview controller and initialize it when the webview is created to the controller of the webview:

    WebViewController? webViewController;
    .
    .
    .
    onWebViewCreated: (controller){
    webViewController = controller;
    }
    

    reload when your button is pressed:

     onPressed: () {
    webViewController!.reload();
    },
    

    Click here to read more about making your webview work excellently