SignalEffect class

A widget that enables executing scoped reactive side-effects inline within the widget tree.

SignalEffect (and its direct type alias SignalListener) allows you to run side-effects (such as showing snackbars, opening dialogs, navigating, or logging metrics) in response to signal updates, without triggering rebuilds of the child widget tree.

The effect callback runs immediately on mount and dynamically tracks any signals accessed within its scope. The underlying subscription is automatically disposed when this widget is removed from the tree. You can optionally return a cleanup function (e.g. void Function()) to run before the next execution or when the widget is disposed.

Dialog and Snackbar Trigger Example

final count = signal(0);

class SnackBarTrigger extends StatelessWidget {
  const SnackBarTrigger({super.key});

  @override
  Widget build(BuildContext context) {
    return SignalEffect(
      effect: (context) {
        // Triggers whenever 'count' updates:
        if (count.value >= 10) {
          ScaffoldMessenger.of(context).showSnackBar(
            SnackBar(content: Text('Limit reached: ${count.value}!')),
          );
        }
        // Optional: return cleanup callback
        return () => print('Cleaning up effect');
      },
      child: ElevatedButton(
        onPressed: () => count.value++,
        child: const Text('Increment and Watch'),
      ),
    );
  }
}

Important

Do not perform synchronous state changes or trigger widget rebuilds directly inside the effect callback to prevent infinite reactive loops.

Inheritance
Available extensions

Constructors

SignalEffect({dynamic effect(BuildContext context)?, @Deprecated('Use effect instead') void callback(BuildContext context)?, required Widget child, String? debugLabel, Key? key})
Creates a SignalEffect widget.
const

Properties

child Widget
The child widget to render.
final
debugLabel String?
Optional debug label for the effect.
final
effect → dynamic Function(BuildContext context)
Gets the effect callback to run inside the reactive effect.
no setter
hashCode int
The hash code for this object.
no setterinherited
key Key?
Controls how one widget replaces another widget in the tree.
finalinherited
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<SignalEffect>
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
toSignalStatefulWidget() SignalStatefulWidget

Available on StatefulWidget, provided by the StatefulWidgetConvertWidgetExtension extension

Converts this StatefulWidget to a SignalStatefulWidget.
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