Cookies
Read and write the WebView's cookies through the controller:
await controller.setCookie(
const WebViewCookie(
name: 'session',
value: 'abc123',
domain: 'example.com',
isSecure: true,
isHttpOnly: true,
// expires: DateTime.now().add(const Duration(days: 7)), // omit for a session cookie
),
);
final cookies = await controller.getCookies('https://example.com/');
await controller.deleteCookie('https://example.com/', 'session'); // one cookie by name
await controller.deleteCookies('https://example.com/'); // every cookie sent to that URL
await controller.clearCookies(); // everythingLoad or reload a page after setCookie for the cookie to be sent.
Clearing website data
Cookies are one kind of website data. To clear the HTTP cache, localStorage / sessionStorage and IndexedDB, use clearWebsiteData:
await controller.clearWebsiteData(); // cache, local storage and IndexedDB
await controller.clearWebsiteData(types: {WebsiteDataType.cache});It does not remove cookies; call clearCookies for those. The data is shared, so it is cleared for every WebView of the app on Android and for every WebView on the same data store on iOS. Android cannot clear localStorage and IndexedDB separately: selecting either one clears all DOM storage of the app. Pages that are already loaded keep what they hold in memory until they reload.
Platform differences
deleteCookieanddeleteCookiesremove what the WebView would send to the URL. iOS deletes exactly the cookiesgetCookiesreports. Android cannot see a cookie's domain or path, so it expires the cookie for the host, its parent domains and every path from/down to the URL's path; a cookie stored for a deeper path than the URL stays. Pass the URL of the page that set the cookie.The cookie store is shared by all WebViews of the app on Android, and by all WebViews that use the default data store on iOS.
On Android
getCookiesonly reportsnameandvalue(CookieManager.getCookiereturns aname=valuelist).domainis the host of the URL,pathis/, and the flags arefalse. iOS reports every attribute.