HopeBuilder<T> class
The main widget for data fetching with automatic caching, loading, error, and refetch handling.
Regular query:
HopeBuilder<List<User>>(
queryKey: ['users'],
fetcher: () => api.getUsers(),
builder: (context, state) {
if (state.isLoading) return CircularProgressIndicator();
if (state.hasError) return Text(state.error.toString());
return UserList(state.data!);
},
)
Infinite / paginated query — just add getNextPageParam:
HopeBuilder<ProductPage>(
queryKey: ['products'],
fetcher: (pageParam) => api.getProducts(cursor: pageParam),
getNextPageParam: (lastPage) => lastPage.nextCursor, // return null when done
builder: (context, state) {
final items = state.pages!.expand((p) => p.items).toList();
return ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) {
if (index == items.length - 1 && state.hasMore == true) {
state.fetchMore();
}
return ProductCard(items[index]);
},
);
},
)
- Inheritance
-
- Object
- DiagnosticableTree
- Widget
- StatefulWidget
- HopeBuilder
Constructors
-
HopeBuilder({Key? key, required List queryKey, required Future<
T> fetcher(dynamic pageParam), required Widget builder(BuildContext context, HopeState<T> state), dynamic getNextPageParam(T lastResult)?, HopeOptions options = const HopeOptions()}) -
const
Properties
-
builder
→ Widget Function(BuildContext context, HopeState<
T> state) -
Builder receives the current HopeState and returns a widget.
final
-
fetcher
→ Future<
T> Function(dynamic pageParam) -
The async function that fetches data.
final
- getNextPageParam → dynamic Function(T lastResult)?
-
Provide this to enable infinite scroll. Receives the last page result
and returns the next page cursor. Return null to signal no more pages.
final
- hashCode → int
-
The hash code for this object.
no setterinherited
- key → Key?
-
Controls how one widget replaces another widget in the tree.
finalinherited
- options → HopeOptions
-
Optional configuration for this query.
final
- queryKey → List
-
Unique identifier for this query. Use a list of serializable values.
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< HopeBuilder< 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