Skip to content

Instantly share code, notes, and snippets.

@VladimirCores
Forked from VB10/future_extension.dart
Created May 6, 2022 14:29
Show Gist options
  • Save VladimirCores/ed09bbeb804c41b227057b23909fe52e to your computer and use it in GitHub Desktop.
Save VladimirCores/ed09bbeb804c41b227057b23909fe52e to your computer and use it in GitHub Desktop.
Future Extension Flutter
extension FutureExtension on Future {
Widget toBuild<T>({Widget Function(T data) onSuccess, Widget onError, dynamic data}) {
return FutureBuilder<T>(
future: this,
initialData: data,
builder: (BuildContext context, AsyncSnapshot snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.waiting:
case ConnectionState.active:
return Center(child: CircularProgressIndicator());
case ConnectionState.done:
if (snapshot.hasData)
return onSuccess(snapshot.data);
else
return onError ?? NotFoundLottie();
break;
default:
return NotFoundLottie();
}
},
);
}
}
class SampleFuture extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Future.value(5).toBuild<int>(
onSuccess: (data) {
return Text(data.toString());
},
onError: Text("Not Found"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment