KaiselStackCodec<R extends KaiselRoute> class abstract

Maps URLs to and from full route stacks.

Unlike KaiselCodec (which handles one URL ↔ one route), a KaiselStackCodec can decode a URL into a stack of more than one frame. This lets you give back-navigation sensible behaviour on deep links: /products/sku-42 can decode to [Home, ProductDetail('sku-42')] so the back button goes home instead of exiting the app.

Convention: encode typically encodes the destination (top of the stack); intermediate routes are implicit in the URL's hierarchy. Two stacks ending in the same route encode to the same URL.

class AppStackCodec implements KaiselStackCodec<AppRoute> {
  const AppStackCodec();

  @override
  Uri encode(List<AppRoute> stack) => switch (stack.last) {
        Home() => Uri(path: '/'),
        ProductDetail(:final id) => Uri(path: '/products/$id'),
        Settings() => Uri(path: '/settings'),
      };

  @override
  List<AppRoute>? decode(Uri uri) => switch (uri.pathSegments) {
        [] || [''] => const [Home()],
        ['products', final id] => [const Home(), ProductDetail(id)],
        ['settings'] => const [Home(), Settings()],
        _ => null,
      };
}
Implementers

Constructors

KaiselStackCodec()
Const constructor so subclasses can be const.
const

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

decode(Uri uri) List<R>?
Decode a URL to a stack. Return null if the URL is unrecognised — the parser will fall back to the configured fallback stack.
encode(List<R> stack) Uri
Encode a stack to a URL.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited