HopeMutationBuilder<TData, TVariables> class

Widget for any write operation — POST, PUT, PATCH, DELETE.

Unlike HopeBuilder, mutations do not run automatically. Call mutate from your UI when the user takes an action.

TData — what the API returns. TVariables — what you pass into mutate.

Example — updating a user:

HopeMutationBuilder<User, Map<String, dynamic>>(
  mutation: (variables) => api.updateUser(variables),
  onSuccess: (user) {
    HopeClient.instance.invalidate(['users']);
    HopeClient.instance.invalidate(['user', user.id]);
  },
  onError: (error) => showSnackBar(context, error.toString()),
  builder: (context, state, mutate) {
    return ElevatedButton(
      onPressed: state.isMutating
          ? null
          : () => mutate({'name': 'Hope', 'email': 'hope@example.com'}),
      child: state.isMutating
          ? CircularProgressIndicator()
          : Text('Save'),
    );
  },
)

Example — deleting with just an id:

HopeMutationBuilder<void, String>(
  mutation: (id) => api.deleteUser(id),
  onSuccess: (_) => HopeClient.instance.invalidate(['users']),
  builder: (context, state, mutate) {
    return IconButton(
      onPressed: () => mutate(userId),
      icon: Icon(Icons.delete),
    );
  },
)
Inheritance

Constructors

HopeMutationBuilder({Key? key, required Future<TData> mutation(TVariables variables), required Widget builder(BuildContext context, HopeMutationState<TData> state, Future<void> mutate(TVariables variables)), void onSuccess(TData data)?, void onError(Object error)?})
const

Properties

builder Widget Function(BuildContext context, HopeMutationState<TData> state, Future<void> mutate(TVariables variables))
Builder receives HopeMutationState and the mutate function.
final
hashCode int
The hash code for this object.
no setterinherited
key Key?
Controls how one widget replaces another widget in the tree.
finalinherited
mutation Future<TData> Function(TVariables variables)
The async function that performs the write operation.
final
onError → void Function(Object error)?
Called when mutation fails.
final
onSuccess → void Function(TData data)?
Called when mutation succeeds. Use this to invalidate related queries.
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<HopeMutationBuilder<TData, TVariables>>
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