有没有办法在 Flutter Web 中创建和读取 cookie?

问题描述 投票:0回答:2

我想知道是否有任何方法可以在 Flutter 中创建和读取 cookie,类似于 Javascript 中的

document.cookie
。这似乎合乎逻辑,因为 Flutter Web 在构建时就变成了 Javascript。

flutter cookies flutter-web
2个回答
15
投票

试试这个

import 'dart:html';

final cookie=document.cookie;

这将返回类似

"key=value; key2=value"
的字符串 如果您需要 Map 数据,您应该使用 split 和 map 方法从其中提取 key: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";

0
投票

只是在上面的答案中添加一件事

如果 dart:html 在 flutter web 中出现错误

使用这个 Dart 包

© www.soinside.com 2019 - 2024. All rights reserved.