getCookies method Null safety

Future<List<Cookie>> getCookies(
  1. {required Uri url,
  2. InAppWebViewController? iosBelow11WebViewController}
)

Gets all the cookies for the given url.

iosBelow11WebViewController is used for getting the cookies (also session-only cookies) using JavaScript (cookies with isHttpOnly enabled cannot be found, see: https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#restrict_access_to_cookies) from the current context of the WebView managed by that controller when you need to target iOS below 11. JavaScript must be enabled in order to work. In this case the url parameter is ignored.

NOTE for iOS below 11.0: All the cookies returned this way will have all the properties to null except for Cookie.name and Cookie.value. If iosBelow11WebViewController is null or JavaScript is disabled for it, it will try to use a HeadlessInAppWebView to get the cookies (session-only cookies and cookies with isHttpOnly enabled won't be found!).

Implementation

Future<List<Cookie>> getCookies(
    {required Uri url,
    InAppWebViewController? iosBelow11WebViewController}) async {
  assert(url.toString().isNotEmpty);

  if (defaultTargetPlatform == TargetPlatform.iOS) {
    var platformUtil = PlatformUtil();
    var version = double.tryParse(await platformUtil.getSystemVersion());
    if (version != null && version < 11.0) {
      return await _getCookiesWithJavaScript(
          url: url, webViewController: iosBelow11WebViewController);
    }
  }

  List<Cookie> cookies = [];

  Map<String, dynamic> args = <String, dynamic>{};
  args.putIfAbsent('url', () => url.toString());
  List<dynamic> cookieListMap =
      await _channel.invokeMethod('getCookies', args);
  cookieListMap = cookieListMap.cast<Map<dynamic, dynamic>>();

  cookieListMap.forEach((cookieMap) {
    cookies.add(Cookie(
        name: cookieMap["name"],
        value: cookieMap["value"],
        expiresDate: cookieMap["expiresDate"],
        isSessionOnly: cookieMap["isSessionOnly"],
        domain: cookieMap["domain"],
        sameSite: HTTPCookieSameSitePolicy.fromValue(cookieMap["sameSite"]),
        isSecure: cookieMap["isSecure"],
        isHttpOnly: cookieMap["isHttpOnly"],
        path: cookieMap["path"]));
  });
  return cookies;
}