context.cookies / context.setCookie()

Access request cookies and set response cookies.

This API can only be used on the server and is only available using the package:jaspr/server.dart import.

context.cookies

With the context.cookies getter you can access the cookies sent with the currently handled request on the server.

class App extends StatelessComponent {
  @override
  Iterable<Component> build(BuildContext context) sync* {
    final sessionCookie = context.cookies['session'];

    if (isValid(sessionCookie)) {
      yield p([text('Welcome back!')]);
    } else {
      yield p([text('Please log in.')]);
    }
  }
}

context.setCookie()

The context.setCookie(String name, String value, ...) method lets you set a cookie in the response.

You can control cookie attributes like expires, maxAge, domain, path, secure, and httpOnly by passing them as named parameters.

class App extends StatelessComponent {
  @override
  Iterable<Component> build(BuildContext context) sync* {
    if (didLogIn) {
      context.setCookie('session', '1234');
    }

    yield p([text('Hello, World!')]);
  }
}