所以我试图在 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之后刷新页面并且页面刷新不会停止。谢谢你。