Reputation: 53
I am wondering if there is any way to create and read cookies in Flutter, similar to document.cookie
in Javascript. It seems logical since Flutter Web becomes Javascript when built.
Upvotes: 5
Views: 13773
Reputation: 61
Just to add one thing to the above answer
if dart:html gives error with flutter web
Upvotes: -1
Reputation: 384
try this
import 'dart:html';
final cookie=document.cookie;
this will return string like "key=value; key2=value"
if you need the data as Map , you should use split and map method to extract the key:value form it
like:
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);
for setting a cookie
document.cookie="key=value";
Upvotes: 16