AfterResponse class

Runs work after the response is sent, so the client does not wait for it. Any code can add work. It does not need the request.

It is off by default. Turn it on once in each isolate, for example in the setup() of a provider. Then add work from a controller, a service or a package:

AfterResponse.enable();
AfterResponse.run(() => mailer.sendWelcome(user));
  • The work of one request runs after the response of that request is sent, one after the other, in the order it was added. The next one starts only when the one before it ends, so work that never ends blocks the rest of the work of that request. It also keeps the upload temp files of that request for ever, as they are deleted after the work.
  • After a RequestTimeout (the answer is 503), the work may run while the late handler still runs. The upload temp files stay until it ends.
  • Outside a request, or when it is off, the work runs soon, after the code that is running now.
  • An error in the work is reported to the exception handler. It never reaches the client and never stops the server.
  • The work is not kept anywhere. When the server stops, work that did not run yet is lost. Use a queue for work that must run.

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

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

Static Properties

enabled → bool
true after enable in this isolate
no setter

Static Methods

disable() → void
Turn it off in this isolate. For tests.
enable() → void
Turn it on in this isolate. Each request then runs in its own zone, so run knows which response to wait for.
flushed() → Future<void>
Internal: ends when the work of the current request has run. Done at once when there was no work.
handlerEnded() → void
Internal: the request handler calls it when the middleware and the controller of the current request have ended. The work waits for it, so a client that leaves early does not start it while the handler still runs.
run(FutureOr<void> work()) → void
Runs work after the response of the current request is sent. Outside a request, or when it is off, work runs soon.
wrap(HttpResponse res, void handle()) → void
Internal: the request handler calls it when it is on. Runs handle in a zone of its own, then the work after res is sent. The work list and the wait for res are made on the first run, so a request that adds no work pays only for the zone.