Dhanaji Yadav
Dhanaji Yadav

Reputation: 1422

Flutter web removes cookies after the browser is closed

I have used document.cookie to save the cookie, it works fine when I am using the browser continuously, but when I close the browser and reopen it, the cookies get removed. help me.

this is code snipit how i m using cookies.

import 'dart:html';

class CookieManager {

  static addToCookie(String key, String value) {
    document.cookie = "$key=$value;";
  }

  static String getCookie(String key) {

    String cookies = document.cookie;
    List<String> listValues = cookies.isNotEmpty ? cookies.split(";") : List();
    String matchVal = "";
    for (int i = 0; i < listValues.length; i++) {
      List<String> map = listValues[i].split("=");
      String _key = map[0].trim();
      String _val = map[1].trim();
      if (key == _key) {
        matchVal = _val;
        break;
      }
    }
    return matchVal;
  }
}

Upvotes: 0

Views: 2711

Answers (1)

Dinmukhamed Isenov
Dinmukhamed Isenov

Reputation: 68

I guess you have to provide a timer for the cookie

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

Upvotes: 2

Related Questions