Webview不断刷新——Flutter InAppWebView

Webview constantly refreshing - Flutter InAppWebView

所以我试图在 Webview 中设置一个 cookie(设置用户令牌),但我遇到了页面不断刷新的问题。这是代码:

InAppWebViewController _webViewController;
  final expiresDate = DateTime.now().add(Duration(days: 3)).millisecondsSinceEpoch;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: DashboardPageTitlesAtom(
        title: widget.title,
        leading: IconButton(
          icon: Icon(Icons.arrow_back),
          onPressed: () {
            Navigator.pop(context);
          },
        ),
        trailing: IconButton(
          icon: Icon(CupertinoIcons.refresh),
          onPressed: _webViewController == null ? null : () => _webViewController.reload(),
        ),
      ),
   
      body: Column(
        children: [
          Divider(
            thickness: 1,
            height: 1,
          ),
          Expanded(
            child: InAppWebView(
                initialOptions: InAppWebViewGroupOptions(
                  crossPlatform: InAppWebViewOptions(
                    javaScriptEnabled: true,
                    clearCache: false,
                  ),
                ),
                onWebViewCreated: (InAppWebViewController controller) async {
                  _webViewController = controller;
                },
                onLoadStop: (InAppWebViewController controller, url) async {
                  try {
                    final token = await getToken();
                    await _webViewController.evaluateJavascript(
                      source:
                          'document.cookie = "accessToken=${token}"; secure=true; httpOnly=false; sameSite=None',
                    );
                    await _webViewController.loadUrl(url: widget.url);
                  } catch (e) {
                    print(e.toString());
                  }
                }),
          ),
        ],
      ),
    );
  }

问题是设置 cookie 的唯一方法是在评估 javascript 后刷新页面,并且页面刷新不会停止。谢谢。

试试 CookieManager

这样调用:

CookieManager _cookieManager = CookieManager.instance();

final expiresDate =
    DateTime.now().add(Duration(days: 3)).millisecondsSinceEpoch;
`enter code here`_cookieManager.setCookie(
  url: "https://flutter.dev/",
  name: "session",
  value: "54th5hfdcfg34",
  domain: ".flutter.dev",
  expiresDate: expiresDate,
  isSecure: true,
);

您正在 onLoadStop 事件中调用 loadUrl 方法。这样你就创建了一个循环周期:

"onLoadStop" -> "loadUrl" -> "onLoadStop" -> "loadUrl" -> "onLoadStop" -> etc.

onLoadStop 被调用时,您正在加载 URL,因此 onLoadStop 将针对新的 URL 请求再次被调用。

要实现您的需要,您应该使用 CookieManager class 并在创建 WebView 时设置 cookie(即在 onWebViewCreated 事件中)。 然后,您使用 URL 请求调用 loadUrl 方法。

在你的场景中,你会:

onWebViewCreated: (controller) async {
  _webViewController = controller;
  try {
    // get the access token
    final token = await getToken();

    // get the CookieManager instance
    CookieManager cookieManager = CookieManager.instance();

    // set the access token
    await cookieManager.setCookie(
      url: widget.url,
      name: "accessToken",
      value: token,
      isSecure: true,
      isHttpOnly: false,
      sameSite: HTTPCookieSameSitePolicy.NONE
    );

    // then load your initial URL here
    await _webViewController.loadUrl(url: widget.url);
  } catch (e) {
    print(e.toString());
  }
},