cookie_jar 1.0.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 94

CookieJar #

build statud Pub 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 #

  cookie_jar: ^1.0.0

Usage #

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(""));

Classes #

SerializableCookie #

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 #

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 #

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(
    ignoreExpires:true, //save/load even cookies that have expired.

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(dir:tempPath);

APIs #

void saveFromResponse(Uri uri, List

Save the cookies for specified 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(dir:"./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.

1.0.1 #

  • add forceInit method for PersistCookieJar
  • handle cookie name conflicts

0.0.1 #

  • Initial version, created by Stagehand


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 =
  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: ^1.0.1

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter pub get

Alternatively, your editor might support pub get or flutter pub 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';
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 Jun 5, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.2
  • pana: 0.13.8-dev

Health suggestions

Fix lib/src/persist_cookie_jar.dart. (-9.08 points)

Analysis of lib/src/persist_cookie_jar.dart reported 19 hints, including:

line 61 col 19: Unnecessary new keyword.

line 77 col 15: Unnecessary new keyword.

line 78 col 31: Unnecessary new keyword.

line 79 col 22: Unnecessary new keyword.

line 82 col 20: Unnecessary new keyword.

Fix lib/src/serializable_cookie.dart. (-1.49 points)

Analysis of lib/src/serializable_cookie.dart reported 3 hints:

line 8 col 24: Unnecessary new keyword.

line 15 col 14: Unnecessary new keyword.

line 22 col 24: Unnecessary new keyword.

Fix lib/src/default_cookie_jar.dart. (-0.50 points)

Analysis of lib/src/default_cookie_jar.dart reported 1 hint:

line 105 col 26: Unnecessary new keyword.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.20.1 <3.0.0
Dev dependencies
pedantic 1.1.0
test ^1.5.1