EngineContext class

The EngineContext is loosely inspired by gin.Context in Go. It wraps Request and Response, holds arbitrary keys/values, tracks errors, and can control flow in a chain of handlers.

Available extensions

Constructors

EngineContext({required Request request, required Response response, List<Middleware>? handlers, Engine? engine, EngineRoute? route, Container? container})
Create a new context around a Request and Response.

Properties

acceptsHtml bool

Available on EngineContext, provided by the EngineContextResponseFormat extension

Whether the request accepts an HTML response.
no setter
bodyBytes Future<Uint8List>
Retrieve the request body as raw bytes.
no setter
clientIP String
Retrieve the IP address of the client making the request.
no setter
container Container
Retrieves the request-scoped container.
no setter
contentLength int
Returns the content length of the request body.
no setter
engine Engine?
The engine that created this context, when one is available.
no setter
engineConfig EngineConfig
Retrieves the engine configuration.
no setter
errors List<EngineError>
Retrieve all errors attached to this context.
no setter
formCacheKey String
The request attribute key used to cache parsed form data.
final
hashCode int
The hash code for this object.
no setterinherited
headers HttpHeaders
Retrieve the request headers.
no setter
host String
Retrieve the request host.
no setter
hostContext Object?
Opaque host context supplied by a portable host adapter, when available.
no setter
id String
Unique identifier for tracking, same as request.id.
final
isAborted bool
Check if we have aborted the chain.
no setter
isClosed bool
Check if the response is closed.
no setter
isUpgraded bool
Indicates that the connection has been upgraded and detached.
no setter
method String
Retrieve the request method.
no setter
multipartFormKey String
The request attribute key used to cache parsed multipart form data.
final
params Map<String, dynamic>
Retrieve route parameters.
no setter
path String
Returns the request path.
no setter
queryCacheKey String
The request attribute key used to cache parsed query parameters.
final
remoteAddr String
Retrieve the remote address of the direct connection.
no setter
request Request
The current HTTP request data.
final
requestedUri Uri
Retrieve the requested URI for the request.
no setter
response Response
Retrieve the response object.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
scheme String
Retrieve the request scheme.
no setter
statusCode int
Returns the current HTTP status code of the response.
no setter
uri Uri
Retrieve the request URI.
no setter
wantsJson bool

Available on EngineContext, provided by the EngineContextResponseFormat extension

Whether the request prefers a JSON response.
no setter

Methods

abort() → void
Abort the remaining handlers; no further ctx.next() calls will proceed.
abortWithError(int statusCode, [String message = '']) → void
Abort the chain immediately and write a specific statusCode plus message.
abortWithStatus(int statusCode, [String message = '']) → void
Abort the chain immediately and write a specific statusCode plus message.
addError(String message, {int? code}) EngineError
Add an error to this context, optionally specifying a type or other metadata.
body() FutureOr<String>
Retrieve the request body as a UTF-8 decoded string.
clear() → void
Clear all data for this request context.
close() Future<void>
Close the response and await the underlying HttpResponse completion.
config<T extends Object>() → T

Available on EngineContext, provided by the EngineContextHelpers extension

Retrieves a typed configuration value for this request.
config<T extends Object>() → T
Resolves an immutable typed application configuration object.
configuration<T extends Object>() → T
Explicit alias for typed configuration lookup.
contains<T>(ContextKey<T> key) bool

Available on EngineContext, provided by the TypedContextState extension

Returns true if key was written to this context, even when its value is null.
contentType() String?
Retrieve the content type of the request.
Retrieve a cookie from the request.
currentLocale([String? defaultLocale]) String

Available on EngineContext, provided by the EngineContextHelpers extension

Returns the locale currently associated with the request.
errorResponse({required int statusCode, required String message, Map<String, dynamic>? jsonBody}) Response

Available on EngineContext, provided by the EngineContextResponseFormat extension

Writes an error response in the format preferred by the request.
fetchOr404<T>(FutureOr<T?> resolver(), {String? message}) Future<T>

Available on EngineContext, provided by the ContextShortcuts extension

Awaits the resolver and returns its value, throwing a NotFoundError when the resolved value is null.
filterFlags(String content) String
Filter flags from a content string.
get<T>(String key) → T?
Retrieve a stored value by key.
getContextData<T>(String key) → T?
Retrieve context-scoped data.
hasAttribute(String key) bool
Returns true if an attribute with key is present, even when its value is null.
Returns the response header named s, or null when it is absent.
html(String content, {int statusCode = 200}) Response

Available on EngineContext, provided by the EngineContextHelpers extension

Writes content as an HTML response with statusCode.
json(Object? data, {int statusCode = 200}) Response

Available on EngineContext, provided by the EngineContextHelpers extension

Writes data as a JSON response with statusCode.
markUpgraded() → void
Marks a transport-level upgrade that was performed by the engine.
mustGet<T>(String key) → T
Retrieve a stored value by key and throw an error if not found.
mustGetParam<T>(String s) → T
Retrieve a required route parameter as type T.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
param(String s) String?
Retrieve a parameter from the route.
query(String s) → dynamic
Retrieve a query parameter from the request.
read<T>(ContextKey<T> key) → T?

Available on EngineContext, provided by the TypedContextState extension

Reads value for key or null if absent or type-mismatch.
redirect(String location, {int statusCode = HttpStatus.found}) Future<Response>

Available on EngineContext, provided by the EngineContextHelpers extension

Sets a redirect response to location with statusCode.
requestHeader(String s) String?
Retrieve a header from the request.
require<T>(ContextKey<T> key) → T

Available on EngineContext, provided by the TypedContextState extension

Reads value for key or throws StateError if key was never written.
requireFound<T>(T? value, {String? message}) → T

Available on EngineContext, provided by the ContextShortcuts extension

Returns value when it is non-null; otherwise throws a NotFoundError.
resetHandlers() → void
Reset the chain index so we can re-run (uncommon).
route(String name, [Map<String, dynamic>? parameters]) String

Available on EngineContext, provided by the EngineContextHelpers extension

Generates a route URL by name.
run() Future<Response>
Helper to start processing the chain from the first handler.
set(String key, dynamic value) → void
Store a value value under key.
setContextData(String key, dynamic value) → void
Store context-scoped data.
setCookie(String name, String value, {int maxAge = 0, String path = '/', String domain = '', bool secure = false, SameSite? sameSite, bool httpOnly = false}) → void
Set a cookie in the response.
setHeader(String s, String t) → void
Set a header in the response.
status(int code) → void
Sets the HTTP status code for the response.
string(String content, {int statusCode = 200}) Response

Available on EngineContext, provided by the EngineContextHelpers extension

Writes content as a text response with statusCode.
toString() String
A string representation of this object.
inherited
trans(String key, {Map<String, dynamic>? replacements, String? locale, bool fallback = true}) Object?

Available on EngineContext, provided by the EngineContextHelpers extension

Translates key using the current request locale.
transChoice(String key, num count, {Map<String, dynamic>? replacements, String? locale}) String

Available on EngineContext, provided by the EngineContextHelpers extension

Translates key using pluralization rules and the supplied count.
upgrade<T>(Future<T> handler(Socket socket), {bool writeHeaders = true}) Future<T>
Upgrades the HTTP connection and yields the underlying socket to the caller.
write(String s) → void
Write a string to the response.
write<T>(ContextKey<T> key, T value) → void

Available on EngineContext, provided by the TypedContextState extension

Writes value for key.

Operators

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