如何将一个值传递给 webview_flutter 的 4 ..loadRequest(Uri.parse(_posUrl))。

huangapple go评论66阅读模式
英文:

How to pass a value to webview_flutter 4 ..loadRequest(Uri.parse(_posUrl))

问题

我应该如何将一个值传递给webview_flutterloadRequest()函数?

我尝试使用initialUrl来传递给webview_flutterloadRequest()函数,但我收到了以下错误消息:

实例成员'_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.

huangapple
  • 本文由 发表于 2023年6月16日 04:20:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76485252.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定