class Month {
static const List<String> short3l = const ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
static String getShort(num i) => Month.short3l[i-1];
}
class ClientCookie{
static Map _readCookie(){
var cookie = new Map();
document.cookie.split(';').forEach((e){
var k = e.indexOf('=');
if(k > 0){
cookie[Uri.decodeComponent(e.substring(0, k))] = Uri.decodeComponent(e.substring(k + 1));
}
});
return cookie;
}
static _writeCookie(Map m){
String sc = '';
m.forEach((String k, v) {
sc = '$sc''$k=$v;';
});
document.cookie = sc;
}
static void setCookie(String name, Object value, { int ms : null }){
var m = new Map();
m[name] = value.toString();
if(ms != null){
DateTime t = new DateTime.fromMillisecondsSinceEpoch(new DateTime.now().millisecondsSinceEpoch + ms);
m['expires'] = '${t.day}-${Month.getShort(t.month)}-${t.year} ${t.hour}:${t.minute}:${t.second}';
}
_writeCookie(m);
}
static bool deleteCookie(String name){
var m = new Map();
m[name]=' ';
DateTime t = new DateTime.now().toUtc();
m['expires'] = '${t.day}-${Month.getShort(t.month)}-${t.year} ${t.hour}:${t.minute}:${t.second}';
_writeCookie(m);
}
static String getCookie(String name){
Map c = _readCookie();
return c.containsKey(name)? c['name'] : null;
}
}
You can use it to delete cookie:ClientCookie.deleteCookie('myCookie');
document.cookie returns String like this '<name>=<value>; <name>=<value>', document.cookie = '$name=$value' creates new cookie file with specified name and value (and optionally with other parameters like EXPIRES, PATH, etc.
Also noticed that value of expires should have strict pattern DD-MMM-YYYY HH:MM:SS and represents time in GMT.
Hope, this helps.Hi all,