首页 > 解决方案 > 如何将过期时间添加到颤动的 Web cookie?

问题描述

我正在使用document.cookie将 cookie 存储在我的网络应用程序中。我想添加“过期”参数并设置 30 天后过期。我怎样才能做到这一点。

  void _addToCookie(String key, String value) {
    document.cookie = "$key=$value;expires=30";
  }

标签: flutterflutter-web

解决方案


使用“max-age”参数而不是“expires”。"max-age" 需要秒;max-age=max-age-in-seconds(例如,60*60*24*365 或 31536000 为一年)

30 天 30×24×60×60 = 2592000。

 void _addToCookie(String key, String value) {
    document.cookie = "$key=$value; max-age=2592000;";
  }

推荐阅读