cookies - 如何在Dart中删除Cookie

标签 cookies dart

在客户端上运行时,如何删除dart中的cookie?

我试图通过将其设置为空字符串来删除它

document.cookie = 'cookie_to_be_deleted=""';

如果我在此行之后打印cookie的值,则会得到以分号分隔的键值对列表,其中包含两个“cookie_to_be_deleted”实例。一个具有我希望删除的原始值,另一个具有其值的空字符串。

最佳答案

试试这个:

Date then = new Date.fromEpoch(0, new TimeZone.utc());
document.cookie = 'cookie_to_be_deleted=; expires=' + then.toString() + '; path=/';

https://gist.github.com/d2m/1935339找到了这些实用程序
/* 
 * dart document.cookie lib
 *
 * ported from
 * http://www.quirksmode.org/js/cookies.html
 *
 */

void createCookie(String name, String value, int days) {
  String expires;
  if (days != null)  {
    Date now = new Date.now();
    Date date = new Date.fromEpoch(now.value + days*24*60*60*1000, new TimeZone.local());
    expires = '; expires=' + date.toString();    
  } else {
    Date then = new Date.fromEpoch(0, new TimeZone.utc());
    expires = '; expires=' + then.toString();
  }
  document.cookie = name + '=' + value + expires + '; path=/';
}

String readCookie(String name) {
  String nameEQ = name + '=';
  List<String> ca = document.cookie.split(';');
  for (int i = 0; i < ca.length; i++) {
    String c = ca[i];
    c = c.trim();
    if (c.indexOf(nameEQ) == 0) {
      return c.substring(nameEQ.length);
    }
  }
  return null;  
}

void eraseCookie(String name) {
  createCookie(name, '', null);
}

关于cookies - 如何在Dart中删除Cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18493639/

相关文章:

node.js - 从 Node.js 服务器文件向浏览器添加 Cookie

codeigniter cookie 过期问题

flutter - 覆盖抽象类Dart中的最终属性

dart - 升级到聚合物 : ^1. 0.0-rc.10 和可反射 : ^0. 5.0 后无法运行应用程序

javascript - 通过 jQuery cookie 脚本删除 cookie

java - 将 cookie 从域传递到子域

security - 为什么 Magento 每个 session 使用 2 个 cookie?

button - 如何在 flutter 中使用 2 个按钮控制抽屉和 endDrawer

android-studio - Flutter 在运行选项卡中禁用系统调试消息

flutter - 如何在Flutter中将图像和其他 View 用作背景