ViewContext class abstract interface

Provides access to HTTP request data and context for view rendering components.

The ViewContext interface serves as a unified access point for all request-related data that view components (RenderableView and RenderableWebView) need during the rendering process. It abstracts away the underlying HTTP request details and provides a clean, type-safe API for accessing various data sources.

Data Sources

The context provides access to multiple layers of request data:

  • Headers: HTTP request headers (e.g., Accept, User-Agent, Authorization)
  • Session: User session data persisted across multiple requests
  • Path Variables: Dynamic segments from URL patterns (e.g., /users/{id})
  • Query Parameters: URL query string parameters (e.g., ?page=1&sort=name)
  • Request Attributes: Server-side attributes set by filters or previous processing
  • Cookies: Client-side cookies sent with the request

Usage in View Components

View components access the context during rendering to dynamically generate content based on the current request state:

@WebView("/users/{id}/profile")
class UserProfileView extends RenderableView {
  @override
  String render(ViewContext context) {
    // Access various data sources
    final userId = context.getPathVariable("id");
    final currentUser = context.getSessionAttribute("currentUser");
    final theme = context.getQueryParam("theme");
    final userAgent = context.getHeader("User-Agent");
    final trackingCookie = context.getCookie("trackingId");
    
    // Use the data to generate dynamic HTML
    return """
    <html>
      <body data-theme="$theme">
        <h1>User Profile: $userId</h1>
        <p>Logged in as: $currentUser</p>
        <p>Browser: $userAgent</p>
      </body>
    </html>
    """;
  }
}

Framework Integration

The framework automatically provides a concrete implementation of ViewContext when invoking view render methods. The context is populated with data from:

  • The current ServerHttpRequest being processed
  • The routing system for path variables
  • The session management system for user data
  • Interceptors and filters for request attributes

Thread Safety

ViewContext implementations are typically request-scoped and not thread-safe. They should only be accessed within the context of a single request processing operation and not shared across threads.

Null Safety

All methods return nullable values, requiring callers to handle cases where the requested data is not present in the current request context.

Implementers

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

getAttribute(String name) Object?
Retrieves a request attribute set by server-side components.
getCookie(String name) String?
Retrieves the value of a cookie sent with the current request.
getHeader(String name) String?
Retrieves the value of the specified HTTP request header.
getPathVariable(String name) String?
Retrieves a path variable value from the current request URL.
getQueryParam(String name) String?
Retrieves a query parameter value from the request URL.
getSessionAttribute(String name) Object?
Retrieves an attribute from the current user's HTTP session.
getViewAttributes() Map<String, Object?>
Returns an unmodifiable view of all stored view attributes.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
setViewAttributes(Map<String, Object?> attributes) → void
Sets multiple attributes in the current view rendering context.
toString() String
A string representation of this object.
inherited

Operators

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

Static Properties

CLASS → Class
Represents the ViewContext type for reflection purposes.
final