NyFutureBuilder
Introduction to NyFutureBuilder
The NyFutureBuilder is a helpful widget for handling Future
's in your Flutter projects.
It will display a loader while the future is in progress, after the Future completes, it will return the data via the child
parameter.
Let's dive into some code.
- We have a Future that returns a String
- We want to display the data on the UI for our user
// 1
Future<String> _findUser() async {
return await NyStorage.read("a_user_from_storage");
}
// 2
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Container(
child: NyFutureBuilder(future: _findUser(), child: (context, data) {
// data = the return value from Future 'findUser()'
return Text(data);
},),
),
),
);
}
This widget will handle the loading on the UI for your users until the future completes.
Customizing the NyFutureBuilder
You can pass the following parameters to the NyFutureBuilder
class to customize it for your needs.
Options:
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: NyFutureBuilder(
future: NyStorage.read("product_name"),
child: (context, data) {
return Text(data);
},
loading: CupertinoActivityIndicator(), // change the default loader
onError: (AsyncSnapshot snapshot) { // handle exceptions thrown from your future.
print(snapshot.error.toString());
return Text("Error");
},
)
),
);
}