toWidget method
Widget
toWidget({
- required Widget onSuccess(
- BuildContext context,
- T data
- Widget onLoading(
- BuildContext context
- Widget onError(
- BuildContext context,
- Object? error
Simplifies usage of FutureBuilder
Implementation
Widget toWidget({
required Widget Function(BuildContext context, T data) onSuccess,
Widget Function(BuildContext context)? onLoading,
Widget Function(BuildContext context, Object? error)? onError,
}) {
return FutureBuilder<T>(
future: this,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return onLoading?.call(context) ?? Center(child: CircularProgressIndicator());
} else if (snapshot.hasError) {
return onError?.call(context, snapshot.error) ??
Center(child: Text("Error: ${snapshot.error}"));
} else if (snapshot.hasData) {
return onSuccess(context, snapshot.data as T);
} else {
return Center(child: Text("No data"));
}
},
);
}