useCellRouter function

CellRouterController useCellRouter()

Returns a reactive router controller for the nearest CellRouterScope.

The returned controller rebuilds the calling hook whenever the mirrored route state changes; its navigation methods delegate to the scope's GoRouter:

class DetailButton extends HookWidget {
  @override
  Widget build(BuildContext context) {
    final router = useCellRouter();
    return TextButton(
      onPressed: () => router.go('/detail/42'),
      child: const Text('Open detail'),
    );
  }
}

Throws:

StateError when no CellRouterScope encloses the calling widget.

Implementation

CellRouterController useCellRouter() {
  final context = useContext();
  final scope = CellRouterScope.of(context);
  final state = useCell<CellRouterState, CellRouterState>(
    cell: scope.stateCell,
    select: (value) => value ?? const CellRouterState(location: '/'),
  );
  return CellRouterController(router: scope.router, state: state);
}