SuperConsumer<T> class

A widget that consumes an RxT or RxNotifier object and rebuilds whenever it changes.

SuperConsumer is a StatefulWidget that listens to changes in an RxT or RxNotifier object and rebuilds its child widget whenever the Rx object's state changes.

The SuperConsumer widget takes a builder function, which is called whenever the Rx object changes. The builder function receives the current BuildContext and the latest state of the Rx object, and returns the widget tree to be built.

Example usage:

CounterNotifier get counterNotifier => Super.init(CounterNotifier());

// ...

SuperConsumer<int>(
  rx: counterNotifier,
  builder: (context, state) {
    return Text('Count: $state');
  },
)

In the above example, a SuperConsumer widget is created and given a RxNotifier<int> object called counterNotifier. Whenever the state of the counterNotifier changes, the builder function is called with the latest state, and it returns a Text widget displaying the count.

Asynchronous State

The SuperConsumer widget can also be used for asynchronous state.

The optional loading parameter can be used to specify a widget to be displayed while an RxNotifier is in loading state.

Example usage:

CounterNotifier get counterNotifier => Super.init(CounterNotifier());

class CounterNotifier extends RxNotifier<int> {
  @override
  int initial() {
    return 0; // Initial state
  }

  Future<void> getData() async {
    toggleLoading(); // set loading to true
    state = await Future.delayed(const Duration(seconds: 3), () => 5);
  }
}

SuperConsumer<int>(
  rx: counterNotifier,
  loading: () => const CircularProgressIndicator();
  builder: (context, state) {
    return Text('Count: $state');
  },
)

As seen above, a SuperConsumer widget is created and given a RxNotifier object called counterNotifier. When the widget is built, if the RxNotifier is in loading state, the loading widget will be displayed. When the asynchronous method completes and the state is updated, the builder function is called with the state.

See also:

  • RxNotifier class from the flutter_super package.
  • RxT class from the flutter_super package.
  • SuperBuilder widget, which rebuilds when the Rx object changes without needing an rx parameter.
Inheritance

Constructors

SuperConsumer({required Widget builder(BuildContext context, T state), required Rx<T> rx, Widget loading()?, Key? key})
A widget that consumes an RxT or RxNotifier object and rebuilds whenever it changes.
const

Properties

builder Widget Function(BuildContext context, T state)
The function that defines the widget tree to be built when the Rx object changes.
final
hashCode int
The hash code for this object.
no setterinherited
key Key?
Controls how one widget replaces another widget in the tree.
finalinherited
loading Widget Function()?
A widget to be displayed while an RxNotifier is in loading state.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
rx Rx<T>
The Rx object to be consumed and listened to for changes.
final

Methods

createElement() StatefulElement
Creates a StatefulElement to manage this widget's location in the tree.
inherited
createState() State<SuperConsumer<T>>
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