英文:
How to pass a value to webview_flutter 4 ..loadRequest(Uri.parse(_posUrl))
问题
我应该如何将一个值传递给webview_flutter
的loadRequest()
函数?
我尝试使用initialUrl
来传递给webview_flutter
的loadRequest()
函数,但我收到了以下错误消息:
实例成员'_posUrl'无法在初始化程序中访问。
这是我的当前代码:
var _posUrl;
_POSwebviewtState(this._posUrl);
WebViewController controller = WebViewController()
..setJavaScriptMode(JavaScriptMode.unrestricted)
..setBackgroundColor(const Color(0x00000000))
..setNavigationDelegate(
NavigationDelegate(
onProgress: (int progress) {
// 更新加载进度条。
},
onPageStarted: (String url) {
log("New $url");
},
onPageFinished: (String url) {},
onWebResourceError: (WebResourceError error) {},
onNavigationRequest: (NavigationRequest request) {
if (request.url.startsWith('https://www.twitter.com/')) {
return NavigationDecision.prevent;
}
return NavigationDecision.navigate;
},
),
)
..loadRequest(Uri.parse(_posUrl));
如何正确传递一个值给loadRequest()
而不遇到此错误?
英文:
How can I pass a value to webview_flutter
's loadRequest()
function?
I'm attempting to use initialUrl
to pass to webview_flutter
's loadRequest()
function, but I'm getting the error message:
The instance member '_posUrl' can't be accessed in an initializer.
Here is my current code:
var _posUrl;
_POSwebviewtState(this._posUrl);
WebViewController controller = WebViewController()
..setJavaScriptMode(JavaScriptMode.unrestricted)
..setBackgroundColor(const Color(0x00000000))
..setNavigationDelegate(
NavigationDelegate(
onProgress: (int progress) {
// Update loading bar.
},
onPageStarted: (String url) {
log("New $url");
},
onPageFinished: (String url) {},
onWebResourceError: (WebResourceError error) {},
onNavigationRequest: (NavigationRequest request) {
if (request.url.startsWith('https://www.twitter.com/')) {
return NavigationDecision.prevent;
}
return NavigationDecision.navigate;
},
),
)
..loadRequest(Uri.parse(_posUrl));
How can I properly pass a value to loadRequest()
without encountering this error?
答案1
得分: 0
尝试将控制器声明为late:
late WebViewController controller = WebViewController()...;
这将使其在对象创建时不被初始化,而是在第一次访问时初始化 - 这将在_posUrl
已初始化并可用之后进行。
英文:
Try declaring the controller as late:
late WebViewController controller = WebViewController()...;
That will make it not being initialised upon object creation, but when it's gonna be accessed for the first time - which is gonna be after _posUrl
has been initialised and available.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论