簡體   English   中英

Null 檢查 WebView 中 null 值上使用的運算符 flutter

[英]Null check operator used on a null value in WebView flutter

當我嘗試從按鈕重新加載 webview 時出現錯誤: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();
                        },

這是我的 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,
                ),

在此處輸入代碼在使用變量之前,使用回退運算符為 null 值設置默認值。

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

在這里,“str”是 null,我們在“str”是 null 的情況下設置具有回退值的回退運算符。

如果您正在使用新更新的 flutter_webview 並收到此錯誤,只需停止當前調試 session 並開始新的調試 session。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM