declarative_async_widget 0.0.1 declarative_async_widget: ^0.0.1 copied to clipboard
FutureWidget and StreamWidget - Declarative FutureBuilder and StreamBuilder
FutureWidget and StreamWidget - Improved FutureBuilder and StreamBuilder. #
This package provides FutureWidget
and StreamWidget
, a widget similar to StreamBuilder
/ FutureBuilder
which is designed to reduce
boilerplate and improve error handling. It is a rewrite of the package async_builder
.
How to use #
1. Add to dependencies
dependencies:
declarative_async_widget: ^latest # replace latest with version number
2. Import
import 'package:declarative_async_widget/declarative_async_widget.dart';
FutureWidget #
The [future] can't be null.
Remember to specify the type, e.g., FutureWidget<int>.noError_noLoading
and not FutureWidget.noError_noLoading
, otherwise a dynamic or a nullable type might be inferred.
Default constructor #
whenData
, whenError
and whenLoading
are required. initialData
can't be assigned since whenLoading
is going to be used instead.
class ExampleWidget extends StatefulWidget {
const ExampleWidget({Key? key}) : super(key: key);
@override
ExampleWidgetState createState() => ExampleWidgetState();
}
class ExampleWidgetState extends State<ExampleWidget> {
final Future<int> _getInt = myFuture(false);
static Future<int> myFuture(bool shouldThrow) async {
await Future.delayed(const Duration(seconds: 4));
if (shouldThrow) {
throw Exception("You can't pass true as a param");
}
return 4;
}
@override
Widget build(BuildContext context) {
return FutureWidget<int>(
future: _getInt,
whenData: (context, data) => Text('My data is: $data'),
whenError: (context, error, stackTrace) => Text('Error was thrown: $error'),
whenLoading: (context) => const Text('Loading...'),
);
}
}
noError
constructor #
There is no whenError
param. Make 100% sure the future can't throw an error, else an AsyncWidgetUnexpectedError
will be thrown at runtime.
class ExampleWidgetState extends State<ExampleWidget> {
final Future<int> _getInt = myFuture();
static Future<int> myFuture() async {
await Future.delayed(const Duration(seconds: 4));
return 4;
}
@override
Widget build(BuildContext context) {
return FutureWidget<int>.noError(
future: _getInt,
whenData: (context, data) => Text('My data is: $data'),
whenLoading: (context) => const Text('Loading...'),
);
}
}
noLoading
constructor #
An initialData
is required. There is therefore no whenLoading
param.
class ExampleWidgetState extends State<ExampleWidget> {
final Future<int> _getInt = myFuture(true);
static Future<int> myFuture(bool shouldThrow) async {
await Future.delayed(const Duration(seconds: 4));
if (shouldThrow) {
throw Exception("You can't pass true as a param");
}
return 4;
}
@override
Widget build(BuildContext context) {
return FutureWidget<int>.noLoading(
future: _getInt,
whenData: (context, data) => Text('My data is: $data'),
initialData: 99999,
whenError: (BuildContext context, Object error, StackTrace? stackTrace) =>
Text('Error was thrown: $error'),
);
}
}
noError_noLoading
constructor #
class ExampleWidgetState extends State<ExampleWidget> {
final Future<int> _getInt = myFuture();
static Future<int> myFuture() async {
await Future.delayed(const Duration(seconds: 4));
return 4;
}
@override
Widget build(BuildContext context) {
return FutureWidget<int>.noError_noLoading(
future: _getInt,
whenData: (context, data) => Text('My data is: $data'),
initialData: 99999,
);
}
}
StreamWidget #
It is similar to FutureWidget
, but uses streams. More examples will be added in the future.
Note #
Complete documentation, tests, a proper example will be added in the future.