Skip to content
Advertisement

Null check operator used on a null value in WebView flutter

please a error when I try to reload my webview from a button : E/flutter (18150): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: Null check operator used on a null value

RaisedButton(
                        padding: const EdgeInsets.symmetric(vertical: 10,horizontal: 30),
                        onPressed: () async {
                          controller.reload();
                        },

this is my WebView

 WebView(
                  initialUrl: "https://wikoget.com",
                  javascriptMode: JavascriptMode.unrestricted,
                  onWebViewCreated: (controller){
                   this.controller=controller;
                  },
                  onPageFinished: (String url) {
                    controller
                        .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(() {
                    controller.loadUrl("about:blank");
                    isError = true;
                  }),
                  gestureNavigationEnabled: true,
                ),

Advertisement

Answer

enter code hereUse the fallback operator to set a default value for null values before using the variable.

String? str; //nullable value
str ??= "Fallback Value";
print(str); //Output: Fallback Value

Here, “str” is null, and we set the fallback operator with fallback value in case of “str” is null.

Advertisement