I want to achieve a behaviour when user do pull to refresh, the indicator is spinning continuously until data is fetch from the network with bloc pattern. In fact i know the reason why this is happing because refresh method is returning immediately. How to block this method until data is fetched from Network. How i can await until data come from Network. Thanks!
Future<void> _refresh() async{
context.read<PostBloc>(RefreshEvent());
}
@override
Widget build(BuildContext context) {
return BlocBuilder<PostBloc, PostStates>(builder: (context, state){
switch (state.status){
case PostStatus.failure:
return const Center(child: Text('failed to fetch posts'));
case PostStatus.success:
if (state.posts.isEmpty) {
return const Center(child: Text('no posts'));
}
return RefreshIndicator(
triggerMode: RefreshIndicatorTriggerMode.onEdge,
onRefresh: () async {
await _refresh();
},
child: ListView.builder(
physics: AlwaysScrollableScrollPhysics(),
itemBuilder: (context, index){
if (index >= state.posts.length){
return BottomLoader();
}
return PostListItem(post: state.posts[index]);
},
itemCount: state.hasReachedMax ? state.posts.length : state.posts.length + 1,
controller: _scrollController,
),
);
case PostStatus.initial:
return const Center(child: CircularProgressIndicator());
}
});
}