context.cookies / context.setCookie()

Access request cookies and set response cookies.


context.cookies

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

dart
class App extends StatelessComponent {
  @override
  Component build(BuildContext context) {
    final sessionCookie = context.cookies['session'];

    if (isValid(sessionCookie)) {
      return p([.text('Welcome back!')]);
    } else {
      return 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.

dart
class App extends StatelessComponent {
  @override
  Component build(BuildContext context) {
    if (didLogIn) {
      context.setCookie('session', '1234');
    }

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