Response class

Represents the HTTP response sent back to the client.

This class orchestrates the response process using specialized components for better maintainability and separation of concerns.

Key features:

  • Modular design with separate components for headers, body, status, and rendering
  • Convenient shortcuts for common operations
  • Proper resource management and response state tracking
Implemented types
Implementers
Available extensions

Constructors

Response(HttpRequest _raw)

Properties

body ResponseBody
Access to body content functionality
no setter
cookieHandler Cookies
Cookie management
no setter
hashCode int
The hash code for this object.
no setterinherited
headers ResponseHeaders
Access to header management functionality
no setter
raw HttpRequest
Raw HttpRequest from Dart SDK
no setter
renderer ResponseRenderer
Access to view rendering functionality
no setter
request Request?
The request object associated with this response.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
sent bool
Whether the response has already been sent.
no setteroverride
statusCode int
Access to response status functionality
no setteroverride
statusManager ResponseStatus
Access to status code management functionality
no setter

Methods

accepted() Response
back({String fallback = '/'}) Future<void>
Redirects back to the previous page.
badRequest() Response
Sets common error status codes
bytes(List<int> bytes, {String contentType = 'application/octet-stream'}) → void
Sends binary data (convenience method).
cache(String value) Response
Sets cache control headers (convenience method)
Sets a cookie (convenience method).
cors({String? allowOrigin, String? allowMethods, String? allowHeaders, String? exposeHeaders, bool allowCredentials = false, int? maxAge}) Response
Sets CORS headers (convenience method)
override
created() Response
download(File file, {String? name, bool inline = false, String? contentType}) Future<void>
Sends a file download response.
empty() → void
Sends an empty response (convenience method).
override
file(File file, {String? contentType}) Future<void>
Sends a file response.
flashInput(Map<String, dynamic> inputData) Response

Available on Response, provided by the ResponseExtensions extension

Flash input data to session for next request inputData Map of old input values
forbidden() Response
format(Map<String, Function> formats) Future<void>
Content negotiation helper.
gzip() Response
Enables Gzip compression for the response.
Adds a response header (convenience method).
override
html(String html) → void
Alias for sendHtml.
internalServerError() Response
json(dynamic data) → void
Alias for sendJson.
override
jsonPretty(dynamic data, {int indent = 2}) → void
Sends a pretty-printed JSON response (convenience method).
noCache() Response
Sets no-cache headers (convenience method)
noContent() Response
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
notFound() Response
ok() Response
Sets common success status codes
problem({required String title, required int status, String? detail, String? type, String? instance, Map<String, dynamic>? extensions}) → void
Sends a Problem Details response (RFC 7807).
override
redirect(String url, {int status = 302}) Future<void>
Redirects to another URL (convenience method).
security({bool enableHsts = false, bool enableCsp = false, bool enableXFrameOptions = true, bool enableXContentTypeOptions = true, String? cspPolicy}) Response
Sets security headers (convenience method)
override
send(String text) → void
Sends a plain text response (convenience method).
override
sendHtml(String html) → void
Sends an HTML response (convenience method).
sendJson(dynamic data) → void
Sends a JSON response (convenience method).
override
sessionPut(String key, dynamic value) Response

Available on Response, provided by the ResponseExtensions extension

Store a value in the session key The session attribute key value The value to store
setRequest(Request request) → void
Sets the request object associated with this response.
setStatusCode(int code) Response
Sets the HTTP status code (convenience method).
override
status(int code) Response
Sets the HTTP status code (legacy method for backward compatibility).
override
stream<T>(Stream<T> stream, {String contentType = 'application/octet-stream', Map<String, String>? headers, List<int> toBytes(T)?}) Future<void>
Streams any type of data to the client (convenience method).
toString() String
A string representation of this object.
inherited
unauthorized() Response
view(String viewName, {Map<String, dynamic> data = const {}}) Future<void>
Renders a view template (convenience method).
withHeaders(Map<String, String> headers) Response
Sets multiple headers (convenience method).
override

Operators

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