resource_widget 1.0.2 resource_widget: ^1.0.2 copied to clipboard
Simplifies usage of the flutter `FutureBuilder` and `StreamBuilder` in combination with `Resource<T>` (of `resource_result` package).
Offers a ResourceFutureBuilder
that handles a Future<Resource<T>>
and handles the result by calling
the passed methods for onSuccess, onLoading and onError depending on the state.
Offers a StreamBuilder<T>
that handles a Stream<Resource<T>>
and handles the result by calling
the passed methods for onSuccess, onLoading and onError depending on the state.
Usage #
Use a ResourceFutureBuilder
like this:
import 'package:flutter/material.dart';
import 'package:resource_widget/resource_widget.dart';
import 'package:resource_result/resource_result.dart';
final Future<Resource<int>> getSomeIntFuture = Future.value(Success(3));
// or final Future<Resource<int>> getSomeIntFuture = Future.value(Failure(Error("I failed"));
final myIntResourceFutureBuilder = ResourceFutureBuilder(
future: getSomeIntFuture,
success: (context, myInt) => Text("My int loaded successfully: $myInt"),
loadingIndicator: (context) => const CircularProgressIndicator(),
/* optional handleError: defaults to red Text with error message */
handleError: (context, error) =>
Text("Failed to get my int :(. ${error.message}"),
);
Use a ResourceStreamBuilder
like this:
final Stream<Resource<int>> someIntStream = Stream.fromIterable(
[Loading(null), Success(3), Failure.fromMessage("Failed")]);
final myIntResourceStreamBuilder = ResourceStreamBuilder(
stream: someIntStream,
success: (context, myInt) => Text("My int loaded successfully: $myInt"),
loadingIndicator: (context) => const CircularProgressIndicator(),
/* optional handleError: defaults to red Text with error message */
handleError: (context, error) =>
Text("Failed to get my int :(. ${error.message}"));