setCookie method Null safety

Future<void> setCookie(
  1. {required Uri url,
  2. required String name,
  3. required String value,
  4. String? domain,
  5. String path = "/",
  6. int? expiresDate,
  7. int? maxAge,
  8. bool? isSecure,
  9. bool? isHttpOnly,
  10. HTTPCookieSameSitePolicy? sameSite,
  11. InAppWebViewController? iosBelow11WebViewController}

Sets a cookie for the given url. Any existing cookie with the same host, path and name will be replaced with the new cookie. The cookie being set will be ignored if it is expired.

The default value of path is "/". If domain is null, its default value will be the domain name of url.

iosBelow11WebViewController could be used if you need to set a session-only cookie using JavaScript (so isHttpOnly cannot be set, see: on the current URL of the WebView managed by that controller when you need to target iOS below 11. In this case the url parameter is ignored.

NOTE for iOS below 11.0: If iosBelow11WebViewController is null or JavaScript is disabled for it, it will try to use a HeadlessInAppWebView to set the cookie (session-only cookie won't work! In that case, you should set also expiresDate or maxAge).


Future<void> setCookie(
    {required Uri url,
    required String name,
    required String value,
    String? domain,
    String path = "/",
    int? expiresDate,
    int? maxAge,
    bool? isSecure,
    bool? isHttpOnly,
    HTTPCookieSameSitePolicy? sameSite,
    InAppWebViewController? iosBelow11WebViewController}) async {
  if (domain == null) domain = _getDomainName(url);


  if (defaultTargetPlatform == TargetPlatform.iOS) {
    var platformUtil = PlatformUtil();
    var version = double.tryParse(await platformUtil.getSystemVersion());
    if (version != null && version < 11.0) {
      await _setCookieWithJavaScript(
          url: url,
          name: name,
          value: value,
          domain: domain,
          path: path,
          expiresDate: expiresDate,
          maxAge: maxAge,
          isSecure: isSecure,
          sameSite: sameSite,
          webViewController: iosBelow11WebViewController);

  Map<String, dynamic> args = <String, dynamic>{};
  args.putIfAbsent('url', () => url.toString());
  args.putIfAbsent('name', () => name);
  args.putIfAbsent('value', () => value);
  args.putIfAbsent('domain', () => domain);
  args.putIfAbsent('path', () => path);
  args.putIfAbsent('expiresDate', () => expiresDate?.toString());
  args.putIfAbsent('maxAge', () => maxAge);
  args.putIfAbsent('isSecure', () => isSecure);
  args.putIfAbsent('isHttpOnly', () => isHttpOnly);
  args.putIfAbsent('sameSite', () => sameSite?.toValue());

  await _channel.invokeMethod('setCookie', args);