Observer class

Rebuilds automatically whenever any observable read inside the builder changes its value. Dependencies are re-discovered on every build, so conditional reads (an if that reads a different observable on each pass) are supported naturally.

Implementation note: rebuild scheduling is implemented on top of State.setState (rather than a bespoke Element subclass) because it gives the same safety guarantees — a mounted check plus a scheduler-phase-aware deferral — with less surface area to maintain.

Reconstrói automaticamente sempre que qualquer observável lido dentro do builder tiver seu valor alterado. As dependências são redescobertas a cada build, portanto leituras condicionais (um if que lê um observável diferente em cada passagem) são suportadas naturalmente.

Nota de implementação: o agendamento de rebuild é feito sobre State.setState (em vez de uma subclasse de Element dedicada), pois oferece as mesmas garantias de segurança — checagem de mounted mais um adiamento ciente da fase do scheduler — com menos superfície para manter.

Example / Exemplo:

final count = 0.obs;
Observer(() => Text('${count.value}'));
Inheritance

Constructors

Observer(Widget builder(), {Key? key, String? name})
Creates an Observer running builder on every rebuild. An optional name is used in debug logs and warnings.
const
Observer.withChild({required Widget builder(BuildContext context, Widget child), required Widget child, Key? key, String? name})
Creates an Observer that rebuilds only the part of the subtree builder itself constructs, reusing the same, already-built child widget on every rebuild instead of reconstructing it — a static child subtree, a common technique for avoiding rebuilds of expensive widgets that don't depend on any observable. child is passed back to builder on every rebuild so it can be placed anywhere in the returned subtree (e.g. wrapped by a Row/Padding/Center that does change).
const

Properties

builder Widget Function()
Builds the widget subtree, reading whichever observables are needed. Unused (and never called) when this Observer was created via Observer.withChild_childBuilder is used instead.
final
hashCode int
The hash code for this object.
no setterinherited
key Key?
Controls how one widget replaces another widget in the tree.
finalinherited
name String?
Optional debug label shown in logs and warnings.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

createElement() StatefulElement
Creates a StatefulElement to manage this widget's location in the tree.
inherited
createState() State<Observer>
Creates the mutable state for this widget at a given location in the tree.
override
debugDescribeChildren() List<DiagnosticsNode>
Returns a list of DiagnosticsNode objects describing this node's children.
inherited
debugFillProperties(DiagnosticPropertiesBuilder properties) → void
Add additional properties associated with the node.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toDiagnosticsNode({String? name, DiagnosticsTreeStyle? style}) DiagnosticsNode
Returns a debug representation of the object that is used by debugging tools and by DiagnosticsNode.toStringDeep.
inherited
toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) String
A string representation of this object.
inherited
toStringDeep({String prefixLineOne = '', String? prefixOtherLines, DiagnosticLevel minLevel = DiagnosticLevel.debug, int wrapWidth = 65}) String
Returns a string representation of this node and its descendants.
inherited
toStringShallow({String joiner = ', ', DiagnosticLevel minLevel = DiagnosticLevel.debug}) String
Returns a one-line detailed description of the object.
inherited
toStringShort() String
A short, textual description of this widget.
inherited

Operators

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