Package selection guidance compiled from public package information, community data, and project usage.
A Dart package that manages cookies for HTTP requests, enabling seamless handling of cookie policies and persistent storage across sessions. It integrates with HTTP clients to automatically store, retrieve, and send cookies based on domain and path rules. Ideal for applications requiring stateful interactions with web services, such as login sessions or user preferences. The package supports multiple platforms including Android, iOS, macOS, Windows, and Linux, ensuring cross-platform consistency in cookie management.
Use cases
Managing session cookies in web APIs
Persisting user login states
Automating cookie handling in HTTP requests
Supporting stateful web interactions
Enabling secure cookie storage
Key features
Automatic cookie persistence
Cross-platform compatibility
Integration with HTTP clients
Policy-based cookie management
Secure storage mechanisms
Suitable for
Web API clients needing session management
Applications requiring long-term cookie storage
Developers building authenticated web services
Cross-platform mobile and desktop apps
Apps with complex cookie policies
Considerations
Requires manual integration with HTTP client instances
May need additional configuration for custom storage
Not suitable for high-security environments without extra safeguards
Limited to basic cookie operations
Depends on underlying HTTP client implementation
Original README
Original README snapshot from the linked source repository. Visit the repository for the latest content.
CookieJar
A cookie manager for http requests in Dart, help you to deal with the cookie policies and persistence.
Get started
Checkout the Migration Guide for breaking changes between versions.
Add dependency
You can use the command to add dio as a dependency with the latest stable version:
$ dart pub add cookie_jar
Or you can manually add cookie_jar into the dependencies section in your pubspec.yaml:
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 cookie container and manager for HTTP requests.
DefaultCookieJar
DefaultCookieJar is a default cookie manager which implements the standard cookie policy declared in RFC.
It saves the cookies in the memory, all cookies will be cleared after the app exited.
To use it:
final cookieJar = 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.
To use it:
// Cookie files will be saved in files in "./cookies/4/"final cookieJar = PersistCookieJar(
ignoreExpires: true, // Save/load even cookies that have expired.
);
Note: When using the [FileStorage] in Flutter apps,
use path_provider to obtain available directories.
Directory tempDir = awaitgetTemporaryDirectory();
final tempPath = tempDir.path;
final cookieJar = PersistCookieJar(
ignoreExpires: true,
storage: FileStorage(tempPath),
);
Storage
You can customize your own storage by extending Storage, see FileStorage for more details.
dio is a powerful HTTP client for Dart/Flutter,
which supports global configuration, interceptors, FormData, request cancellation,
file uploading/downloading, timeout, and custom adapters etc.
dio also supports to manage cookies with cookie_jar
using dio_cookie_manager.
For example:
import'package:dio/dio.dart';
import'package:dio_cookie_manager/dio_cookie_manager.dart';
import'package:cookie_jar/cookie_jar.dart';
voidmain() async {
final dio = Dio();
final cookieJar = CookieJar();
dio.interceptors.add(CookieManager(cookieJar));
await dio.get('https://pub.dev/');
// Print cookiesprint(await cookieJar.loadForRequest(Uri.parse('https://pub.dev/')));
// Another request with the cookie.await dio.get("https://pub.dev/");
}