lore_emu
lore_emu

Reputation: 53

Is there a way to create and read cookies in Flutter Web?

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

Answers (2)

Usama Kabir
Usama Kabir

Reputation: 61

Just to add one thing to the above answer

if dart:html gives error with flutter web

use this Dart package

Upvotes: -1

Ali Ammar
Ali Ammar

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

Related Questions