cookie_jar 0.0.6

  • Example
  • Installing
  • Versions
  • 88


build statud Pub coverage support

A cookie manager for http requests in Dart, by which you can deal with the complex cookie policy and persist cookies easily.

Add dependency

  dio: ^0.0.2


A simple usage example:

import 'package:cookie_jar/cookie_jar.dart';
void main() async {
  List<Cookie> cookies = [new Cookie("name", "wendux"),new Cookie("location", "china")];
  var cj = new CookieJar();
  //Save cookies   
  cj.saveFromResponse(Uri.parse(""), cookies);
  //Get cookies  
  List<Cookie> results = cj.loadForRequest(Uri.parse(""));



This class is a wrapper for Cookie class. Because the Cookie class doesn't support Json serialization, for the sake of persistence, we use this class instead of it.


CookieJar is a default cookie manager which implements the standard cookie policy declared in RFC. CookieJar saves the cookies in RAM, so if the application exit, all cookies will be cleared. A example as follow:

var cj= new CookieJar();


PersistCookieJar is a cookie manager which implements the standard cookie policy declared in RFC. PersistCookieJar persists the cookies in files, so if the application exit, the cookies always exist unless call delete explicitly. A example as follows:

// Cookie files will be saved in "./cookies"
var cj=new PersistCookieJar("./cookies");

Note: In Flutter, File system is different from PC, you can use path_provider package to get the path :

// API `getTemporaryDirectory` is from "path_provider" package.
Directory tempDir = await getTemporaryDirectory();
String tempPath = tempDir.path;
CookieJar cj=new PersistCookieJar(tempPath);


void saveFromResponse(Uri uri, List<Cookie> cookies);

Save the cookies for specified uri.

List<Cookie> loadForRequest(Uri uri);

Load the cookies for specified uri.

delete(Uri uri,[bool withDomainSharedCookie = false] )

Delete cookies for specified uri. This API will delete all cookies for the, it will ignored the uri.path.

If withDomainSharedCookie is true , will delete the domain-shared cookies.

Note: This API is only available in PersistCookieJar class.

Working with HttpClient

Using CookieJar or PersistCookieJar manages HttpClient 's request/response cookies is very easy:

var cj=new CookieJar();
request= await httpClient.openUrl(options.method, uri);
response= await request.close();
cj.saveFromResponse(uri, response.cookies);

Working with dio

dio is a powerful Http client for Dart, which supports Interceptors, Global configuration, FormData, File downloading, Timeout etc. And dio supports to manage cookies with cookie_jar, the simple example is:

var dio = new Dio();
dio.cookieJar=new PersistCookieJar("./cookies");
Response<String>  response = await dio.get("");

More details about dio see : .

This open source project authorized by , and the license is MIT.

Features and bugs

Please file feature requests and bugs at the issue tracker.


  • Initial version, created by Stagehand


import 'dart:async';
import 'dart:io';

import 'package:cookie_jar/cookie_jar.dart';

void main() async {
  final List<Cookie> cookies = <Cookie>[
    new Cookie('name', 'wendux'),
    new Cookie('location', 'china'),
  final List<Cookie> cookiesExpired = <Cookie>[
    new Cookie('name', 'wendux')..maxAge = 1,
    new Cookie('location', 'china')..expires = new Duration(hours: 1)),
  final Directory dir = new Directory('./example/.cookies');
  await dir.create(recursive: true);
  final CookieJar cj = new CookieJar();
  //var cj=new PersistCookieJar('./example/.cookies');
  cj.saveFromResponse(Uri.parse(''), cookies);
  List<Cookie> results = cj.loadForRequest(Uri.parse(''));
  assert(results.length == 2);
  results = cj.loadForRequest(Uri.parse(''));
  assert(results.length == 2);
  results = cj.loadForRequest(Uri.parse(''));
  cj.saveFromResponse(Uri.parse(''), cookiesExpired);
  results = cj.loadForRequest(Uri.parse(''));
  assert(results.length == 2);
  await new Future<void>.delayed(const Duration(seconds: 2), () {
    results = cj.loadForRequest(Uri.parse(''));
    assert(results.length == 1);

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  cookie_jar: ^0.0.6

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter packages get

Alternatively, your editor might support pub get or flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:cookie_jar/cookie_jar.dart';
Version Uploaded Documentation Archive
0.0.6 Aug 20, 2018 Go to the documentation of cookie_jar 0.0.6 Download cookie_jar 0.0.6 archive
0.0.5 Aug 20, 2018 Go to the documentation of cookie_jar 0.0.5 Download cookie_jar 0.0.5 archive
0.0.4 May 23, 2018 Go to the documentation of cookie_jar 0.0.4 Download cookie_jar 0.0.4 archive
0.0.3 Apr 27, 2018 Go to the documentation of cookie_jar 0.0.3 Download cookie_jar 0.0.3 archive
0.0.2 Apr 26, 2018 Go to the documentation of cookie_jar 0.0.2 Download cookie_jar 0.0.2 archive
0.0.1 Apr 26, 2018 Go to the documentation of cookie_jar 0.0.1 Download cookie_jar 0.0.1 archive
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Oct 26, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0
  • pana: 0.12.5


Detected platforms: Flutter, other

Primary library: package:cookie_jar/cookie_jar.dart with components: io.

Health suggestions

Format lib/src/default_cookie_jar.dart.

Run dartfmt to format lib/src/default_cookie_jar.dart.

Format lib/src/persist_cookie_jar.dart.

Run dartfmt to format lib/src/persist_cookie_jar.dart.

Format lib/src/serializable_cookie.dart.

Run dartfmt to format lib/src/serializable_cookie.dart.

Maintenance suggestions

Package is pre-v0.1 release. (-10 points)

While there is nothing inherently wrong with versions of 0.0.*, it usually means that the author is still experimenting with the general direction of the API.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.20.1 <4.0.0
Dev dependencies
coverage ^0.9.3
test ^0.12.0