A custom ValueNotifier and ValueListenableBuilder that support asynchronus value handling.
Add this to pubspec.yaml
dependencies:
sura_manager: ^0.1.4
Property |
description |
default |
futureFunction |
a function to run and return data |
null |
reloading |
Reset a state to loading or not when you call refresh or asyncOperation |
true |
onSuccess |
a callback function called after operation is success |
null |
onDone |
a callback function called after operation is completely done, similar to finally in try-catch |
null |
onError |
a callback function called after operation has an error |
null |
field |
description |
data |
current data in the Manager |
error |
error in the Manager |
hasData |
check if our Manager has a data |
hasError |
check if our Manager has an error |
future |
future field of the current futureFunction |
Method |
description |
when |
A method similar to FutureManagerBuilder |
asyncOperation |
run futureFunction that will return a data to our Manager |
refresh |
call the asyncOperation again. we have to assign futureFunction from the constructor or call asyncOperation once to run this method, otherwise it will log an error |
updateData |
a method to update data in our Manager |
modifyData |
a method to update data in our Manager with data callback, prefer using this method to update data. |
resetData |
reset eveything to loading or null state |
addError |
add error into our manager |
class _HomePageState extends State<NewPage> {
FutureManager<int> dataManager = FutureManager();
@override
void initState() {
dataManager.asyncOperation(()async{
await Future.delayed(Duration(seconds: 2));
//Add 10 into our dataManager
return 10;
});
super.initState();
}
@override
Widget build(BuildContext context) {
//Use with FutureManagerBuilder
return Scaffold(
appBar: AppBar(
actions: [
IconButton(
icon:Icon(Icons.refresh),
onPressed:(){
//call our asyncOperation again
dataManager.refresh();
},
)
]
),
body: FutureManagerBuilder<int>(
futureManager: dataManager,
error: (error) => YourErrorWidget(),
loading: YourLoadingWidget(),
ready: (context, data){
//result: My data: 10
return Text("My data: ${data}"),
}
),
);
}
}
Property |
description |
default |
futureManager |
our FutureManager object |
required |
ready |
A widget builder show when [FutureManager] has a data |
required |
loading |
A widget show when [FutureManager] state is loading |
CircularProgressIndicator |
error |
A widget show when [FutureManager] state is error |
Text(error.toString()) |
onError |
A callback function that call when [FutureManager] state is error |
null |
onReady |
A callback function that call when [FutureManager] state has a data |
null |
onRefreshing |
A widget to show on top of this widget when refreshing |
null |