ZetCode

Dart Cookie

last modified October 18, 2026

The Cookie class in Dart represents an HTTP cookie, used for maintaining state between client and server. It's part of the dart:io library for server-side applications.

Cookies store small pieces of data that persist across requests. They're essential for sessions, authentication, and user tracking in web apps.

Basic Definition

A Cookie object contains name-value pairs and optional attributes. These include expiration, domain, path, security flags, and same-site policy.

The class provides methods to parse cookie headers and format cookies for HTTP. It handles both server-side cookie creation and client-side cookie parsing.

Creating a Basic Cookie

This example shows how to create a simple cookie with just a name and value.

main.dart
import 'dart:io';

void main() {
  var cookie = Cookie('session_id', 'abc123xyz456');
  
  print('Name: ${cookie.name}');
  print('Value: ${cookie.value}');
  print('ToString: $cookie');
}

We create a Cookie with a name and value. The toString() method formats it properly for HTTP headers. This is the minimal cookie configuration; Dart sets HttpOnly by default.

$ dart main.dart
Name: session_id
Value: abc123xyz456
ToString: session_id=abc123xyz456; HttpOnly

Cookie with Attributes

This example demonstrates setting various cookie attributes like expiration.

main.dart
import 'dart:io';

void main() {
  var cookie = Cookie('preferences', 'dark_mode=true');
  
  cookie.domain = 'example.com';
  cookie.path = '/settings';
  cookie.expires = DateTime.utc(2025, 5, 4, 14, 32, 10);
  cookie.httpOnly = true;
  cookie.secure = true;
  
  print(cookie);
}

We configure a cookie with domain restriction, path, expiration date, and security flags. These attributes control cookie behavior in browsers.

$ dart main.dart
preferences=dark_mode=true; Expires=Sun, 04 May 2025 14:32:10 GMT; Domain=example.com; Path=/settings; Secure; HttpOnly

Parsing Cookie Headers

This example shows how to parse a Set-Cookie header string into a Cookie object.

main.dart
import 'dart:io';

void main() {
  var header = 'user_token=xyz789; Max-Age=3600; Path=/; Secure; SameSite=Lax';
  
  var cookie = Cookie.fromSetCookieValue(header);
  
  print('Name: ${cookie.name}');
  print('Value: ${cookie.value}');
  print('MaxAge: ${cookie.maxAge}');
  print('Path: ${cookie.path}');
  print('Secure: ${cookie.secure}');
  print('SameSite: ${cookie.sameSite}');
}

The fromSetCookieValue constructor parses HTTP Set-Cookie headers. It extracts all attributes automatically into the Cookie object properties.

$ dart main.dart
Name: user_token
Value: xyz789
MaxAge: 3600
Path: /
Secure: true
SameSite: SameSite=Lax

Multiple Cookies in Headers

This example demonstrates working with multiple cookies in HTTP headers.

main.dart
import 'dart:io';

void main() {
  var cookieHeader = 'session=abc123; lang=en_US; theme=dark';
  
  var cookies = cookieHeader.split(';').map((part) {
    var pair = part.trim().split('=');
    return Cookie(pair[0], pair[1])..httpOnly = false;
  }).toList();
  
  for (var cookie in cookies) {
    print('${cookie.name}: ${cookie.value}');
  }
  
  // Modify one cookie
  cookies[1].value = 'fr_FR';
  print('\nModified: ${cookies.map((c) => c.toString()).join('; ')}');
}

We parse a Cookie header containing multiple name-value pairs. Each pair is turned into a Cookie object, and the objects are serialized back into a Cookie header with their toString methods.

$ dart main.dart
session: abc123
lang: en_US
theme: dark

Modified: session=abc123; lang=fr_FR; theme=dark

Cookie Security Features

This example shows security-related cookie attributes like SameSite and HttpOnly.

main.dart
import 'dart:io';

void main() {
  var secureCookie = Cookie('auth_token', 'secret123');
  
  secureCookie.httpOnly = true;
  secureCookie.secure = true;
  secureCookie.sameSite = SameSite.strict;
  secureCookie.maxAge = 1800; // 30 minutes
  
  print('Secure cookie: $secureCookie');
  
  // For sensitive operations
  var csrfCookie = Cookie('csrf_token', 'r4nd0mT0k3n');
  csrfCookie.sameSite = SameSite.lax;
  
  print('CSRF cookie: $csrfCookie');
}

We configure security flags to protect against XSS and CSRF attacks. HttpOnly prevents JavaScript access, while SameSite restricts cross-site requests.

$ dart main.dart
Secure cookie: auth_token=secret123; Max-Age=1800; Secure; HttpOnly; SameSite=Strict
CSRF cookie: csrf_token=r4nd0mT0k3n; HttpOnly; SameSite=Lax

Best Practices

Source

Dart Cookie Documentation

This tutorial covered Dart's Cookie class with examples showing creation, parsing, security features, and HTTP header handling for web development.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Dart tutorials.