ContextProperty<T extends Object> class

Manages a piece of data associated with a specific Request.

ContextProperty allows middleware or other parts of the request handling pipeline to store and retrieve data scoped to a single request. It uses an Expando internally, keyed by a token from the Request, to ensure that data does not leak between requests.

This is useful for passing information like authenticated user objects, request-specific configurations, or other contextual data through different layers of an application.

Example:

// Define a context property for a user object.
final _currentUserProperty = ContextProperty<User>('currentUser');

// In a middleware, set the user for the current request.
void authMiddleware(Request context, User user) {
  _currentUserProperty[context] = user;
}

// Later, in a handler, retrieve the user.
User? getCurrentUser(Request context) {
  return _currentUserProperty[context];
}

// Maybe create an extension method for convenience.
extension on Request {
  User get currentUser => _currentUserProperty.get(this);
}

Constructors

ContextProperty([String? _debugName])
Creates a new ContextProperty.

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

get(Request request) → T
Retrieves the value associated with the given request.
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
operator [](Request request) → T?
Retrieves the value associated with the given request, or null if no value is set.
operator []=(Request request, T? value) → void
Sets the value for the given request.