StateSelector<S, U> class

A widget that builds itself based on a part of the state from a StateReadable.

StateSelector allows you to optimize rebuilds by only rebuilding when a specific part of the state changes, selected by the selector function.

{@tool snippet} This example shows how to use StateSelector to only rebuild when a specific property of the state changes:

class UserForm extends StatelessWidget {
  final UserController userController;

  const UserForm({
    required this.userController,
    super.key,
  });

  @override
  Widget build(BuildContext context) => StateSelector<UserState, bool>(
    stateReadable: userController,
    selector: (state) => state.isLoading,
    builder: (context, isLoading, _) => isLoading
        ? const CircularProgressIndicator()
        : const Text('User loaded'),
  );
}

// A state readable implementation for user
class UserController implements StateReadable<UserState> {
  // Implementation details...

  @override
  UserState get state => const UserState(); // Example implementation

  @override
  Stream<UserState> get stream => Stream.empty(); // Example implementation
}

// Assume we have a complex state with multiple properties
class UserState {
  final String name;
  final bool isLoading;
  final List<String> permissions;

  const UserState({
    this.name = '',
    this.isLoading = false,
    this.permissions = const <String>[],
  });
}

{@end-tool}

See also:

  • StateBuilder, which rebuilds the UI when the entire state changes.
  • StateListener, which performs side effects in response to state changes.
  • StateConsumer, which combines both rebuilding and side effects.
Inheritance

Constructors

StateSelector({required StateReadable<S> stateReadable, required StateWidgetSelector<S, U> selector, required StateWidgetBuilder<U> builder, Widget? child, Key? key})
Creates a new StateSelector.
const

Properties

builder StateWidgetBuilder<U>
The builder that builds a widget based on the selected value.
final
child Widget?
The child of the widget.
final
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
selector StateWidgetSelector<S, U>
The function that extracts a value from the state.
final
stateReadable → StateReadable<S>
The source of the state.
final

Methods

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