@@ -4,7 +4,7 @@ import 'dart:io';
44import 'package:flutter/services.dart' ;
55
66class WebviewCookieManager {
7- static const MethodChannel _channel = const MethodChannel ('webview_cookie_manager' );
7+ static const _channel = MethodChannel ('webview_cookie_manager' );
88
99 /// Creates a [CookieManager] -- returns the instance if it's already been called.
1010 factory WebviewCookieManager () {
@@ -17,41 +17,42 @@ class WebviewCookieManager {
1717
1818 /// Gets whether there are stored cookies
1919 Future <bool > hasCookies () {
20- return _channel.invokeMethod <bool >('hasCookies' ).then <bool >((bool result) => result);
20+ return _channel
21+ .invokeMethod <bool >('hasCookies' )
22+ .then <bool >((bool result) => result);
2123 }
2224
23- /// Read out all cookies, or all cookies for a [currentUrl] when provided
24- Future <List <Cookie >> getCookies (String currentUrl) {
25- return _channel.invokeListMethod <Map <dynamic , dynamic >>(
26- 'getCookies' ,
27- < dynamic , dynamic > {
28- 'url' : currentUrl,
29- },
30- ).then <List <Cookie >>((List <Map <dynamic , dynamic >> results) {
31- return results.map ((Map <dynamic , dynamic > result) {
32- final Cookie c = Cookie (result['name' ], removeInvalidCharacter (result['value' ]));
33- // following values optionally work on iOS only
34- c.path = result['path' ];
35- c.domain = result['domain' ];
36- c.secure = result['secure' ];
37- c.httpOnly = result['httpOnly' ];
25+ /// Read out all cookies, or all cookies for a [url] when provided
26+ Future <List <Cookie >> getCookies (String url) {
27+ return _channel.invokeListMethod <Map >('getCookies' , {
28+ 'url' : url
29+ }).then ((results) => results.map ((Map result) {
30+ final c =
31+ Cookie (result['name' ], removeInvalidCharacter (result['value' ]))
32+ // following values optionally work on iOS only
33+ ..path = result['path' ]
34+ ..domain = result['domain' ]
35+ ..secure = result['secure' ]
36+ ..httpOnly = result['httpOnly' ];
3837
39- if (result['expires' ] != null ) {
40- c.expires = DateTime .fromMillisecondsSinceEpoch ((result['expires' ] * 1000 ).toInt ());
41- }
38+ if (result['expires' ] != null ) {
39+ c.expires = DateTime .fromMillisecondsSinceEpoch (
40+ (result['expires' ] * 1000 ).toInt ());
41+ }
4242
43- return c;
44- }).toList ();
45- });
43+ return c;
44+ }).toList ());
4645 }
4746
4847 /// Remove cookies with [currentUrl] for IOS and Android
4948 Future <void > removeCookie (String currentUrl) async {
50- final List <Cookie > listCookies = await getCookies (currentUrl);
51- clearCookies ();
52- final List <Cookie > serializedCookies =
53- listCookies.where ((element) => ! currentUrl.contains (element.domain)).toList ();
54- setCookies (serializedCookies);
49+ final listCookies = await getCookies (currentUrl);
50+ final serializedCookies = listCookies
51+ .where ((element) => currentUrl.contains (element.domain))
52+ .toList ();
53+ serializedCookies
54+ .forEach ((c) => c.expires = DateTime .fromMicrosecondsSinceEpoch (0 ));
55+ await setCookies (serializedCookies);
5556 }
5657
5758 /// Remove all cookies
@@ -61,8 +62,8 @@ class WebviewCookieManager {
6162
6263 /// Set [cookies] into the web view
6364 Future <void > setCookies (List <Cookie > cookies) {
64- final List < Map < String , dynamic >> transferCookies = cookies.map ((Cookie c) {
65- final Map < String , dynamic > output = < String , dynamic > {
65+ final transferCookies = cookies.map ((Cookie c) {
66+ final output = < String , dynamic > {
6667 'name' : c.name,
6768 'value' : c.value,
6869 'path' : c.path,
@@ -83,7 +84,7 @@ class WebviewCookieManager {
8384
8485 String removeInvalidCharacter (String value) {
8586 // Remove Invalid Character
86- String valueModified = value.replaceAll ('\\ "' , "'" );
87+ var valueModified = value.replaceAll ('\\ "' , "'" );
8788 valueModified = valueModified.replaceAll (String .fromCharCode (32 ), "" );
8889 return valueModified;
8990 }
0 commit comments