InitBuilder<T> class abstract

A widget that initializes a value only when its configuration changes, useful for safe creation of async tasks.

The default constructor takes a getter, builder, and disposer. The getter function is called to initialize the value used in builder. In this case InitBuilder only re-initializes the value if the getter function changes, you should not pass it an anonymous function directly.

Alternative constructors InitBuilder.arg to InitBuilder.arg7 can be used to pass arguments to the getter, these will re-initialize the value if either getter or the arguments change.

The basic usage of this widget is to make a separate function outside of build that starts the task and then pass it to InitBuilder, for example:

static Future<int> getNumber() async => ...;

Widget build(context) => InitBuilder<int>(
  getter: getNumber,
  builder: (context, future) => AsyncBuilder<int>(
    future: future,
    builder: (context, value) => Text('$value'),
  ),
);

You may also want to pass arguments to the getter, for example to query shared preferences:

final String prefsKey;

Widget build(context) => InitBuilder.arg<String, String>(
  getter: sharedPrefs.getString,
  arg: prefsKey,
  builder: (context, future) => AsyncBuilder<String>(
    future: future,
    builder: (context, value) => Text('$value'),
  ),
);
Inheritance

Constructors

InitBuilder({Key? key, required ValueBuilderFn<T> builder, required ValueGetter<T> getter, ValueSetter<T>? disposer})
Factory constructor for a basic InitBuilder.
factory
InitBuilder.base({Key? key, required ValueBuilderFn<T> builder, ValueSetter<T>? disposer})
Base constructor for anything that implements InitBuilder.
const

Properties

builder ValueBuilderFn<T>
Builder that is called with a previously initialized value.
final
disposer ValueSetter<T>?
Function that is called with the value when the InitBuilder is being disposed.
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

Methods

createElement() StatefulElement
Creates a StatefulElement to manage this widget's location in the tree.
inherited
createState() → _InitBuilderState<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
initValue() → T
Called by the widget state to initialize the value.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
shouldInit(covariant InitBuilder<T> other) bool
Returns true if the value should be re-initialized after a rebuild.
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

Static Methods

arg<T, A>({Key? key, required ValueBuilderFn<T> builder, required A arg, required T getter(A), ValueSetter<T>? disposer}) InitBuilder<T>
Constructor for one argument getters.
arg2<T, A1, A2>({Key? key, required ValueBuilderFn<T> builder, required A1 arg1, required A2 arg2, required T getter(A1, A2), ValueSetter<T>? disposer}) InitBuilder<T>
Constructor for two argument getters.
arg3<T, A1, A2, A3>({Key? key, required ValueBuilderFn<T> builder, required A1 arg1, required A2 arg2, required A3 arg3, required T getter(A1, A2, A3), ValueSetter<T>? disposer}) InitBuilder<T>
Constructor for three argument getters.
arg4<T, A1, A2, A3, A4>({Key? key, required ValueBuilderFn<T> builder, required A1 arg1, required A2 arg2, required A3 arg3, required A4 arg4, required T getter(A1, A2, A3, A4), ValueSetter<T>? disposer}) InitBuilder<T>
Constructor for four argument getters.
arg5<T, A1, A2, A3, A4, A5>({Key? key, required ValueBuilderFn<T> builder, required A1 arg1, required A2 arg2, required A3 arg3, required A4 arg4, required A5 arg5, required T getter(A1, A2, A3, A4, A5), ValueSetter<T>? disposer}) InitBuilder<T>
Constructor for five argument getters.
arg6<T, A1, A2, A3, A4, A5, A6>({Key? key, required ValueBuilderFn<T> builder, required A1 arg1, required A2 arg2, required A3 arg3, required A4 arg4, required A5 arg5, required A6 arg6, required T getter(A1, A2, A3, A4, A5, A6), ValueSetter<T>? disposer}) InitBuilder<T>
Constructor for six argument getters.
arg7<T, A1, A2, A3, A4, A5, A6, A7>({Key? key, required ValueBuilderFn<T> builder, required A1 arg1, required A2 arg2, required A3 arg3, required A4 arg4, required A5 arg5, required A6 arg6, required A7 arg7, required T getter(A1, A2, A3, A4, A5, A6, A7), ValueSetter<T>? disposer}) InitBuilder<T>
Constructor for seven argument getters.