我想实现当用户拉动刷新时的行为,指示器不断旋转,直到以块模式从网络获取数据。事实上,我知道发生这种情况的原因,因为刷新方法立即返回。如何阻止此方法直到从网络获取数据。我如何等待数据来自网络。谢谢!
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 …Run Code Online (Sandbox Code Playgroud) import 'package:flutter/material.dart';
void main() => runApp(MaterialApp(
home: Scaffold(
appBar: AppBar(
toolbarHeight: 50.0,
title: const Text(
'This is the title !',
style: TextStyle(color: Colors.black),
),
backgroundColor: Colors.amber,
),
),
));
Run Code Online (Sandbox Code Playgroud)