有没有一种方法可以在Flutter Web中创建和读取Cookie?

发布于 2025-01-24 04:17:35 字数 126 浏览 2 评论 0原文

我想知道是否有任何方法可以在flutter中创建和读取cookie,类似于document.cookie在JavaScript中。这似乎是合乎逻辑的,因为构建时Flutter Web成为JavaScript。

I am wondering if there is any way to create and read cookies in Flutter, similar to document.cookie in Javascript. It seems logical since Flutter Web becomes Javascript when built.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

表情可笑 2025-01-31 04:17:35

尝试此

导入'dart:html';

final cookie = document.cookie;

此将返回字符串,如“ key = value; key2 = value”
如果需要数据作为映射,则应使用拆分和映射方法提取键:值形式为

import 'dart:html';

final cookie = document.cookie!;
final entity = cookie.split("; ").map((item) {
  final split = item.split("=");
  return MapEntry(split[0], split[1]);
});
final cookieMap = Map.fromEntries(entity);

用于设置cookie

document.cookie="key=value";

try this

import 'dart:html';

final cookie=document.cookie;

this will return string like "key=value; key2=value"
if you need the data as Map , you should use split and map method to extract the key:value form it

like:

import 'dart:html';

final cookie = document.cookie!;
final entity = cookie.split("; ").map((item) {
  final split = item.split("=");
  return MapEntry(split[0], split[1]);
});
final cookieMap = Map.fromEntries(entity);

for setting a cookie

document.cookie="key=value";
最冷一天 2025-01-31 04:17:35

如果DART

:HTML给出flutter Web

使用此dart软件包

Just to add one thing to the above answer

if dart:html gives error with flutter web

use this Dart package

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文