本文介绍了ConnectionState始终在等待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么 connectionState
从 pageB ???
页面A
@override
void initState() {
super.initState();
Future.delayed(Duration.zero, () {
_bloc.getList(context);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Title'),
),
body: WillPopScope(
child: RefreshIndicator(
key: _refreshIndicatorKey,
onRefresh: _refresh,
child: Container(
child: StreamBuilder<List<ABC>>(
stream: _bloc.abcStream,
builder: (context, snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.waiting:
return Center(
child: Image.asset(
'assets/loading.gif',
width: 200.0,
height: 200.0,
),
);
break;
case ConnectionState.active:
if (snapshot.data.isEmpty) {
return Container(
child: Text('empty'),
);
} else {
return ListView.builder(
.......
}
break;
default:
return Text("Error");
}
}))),
onWillPop: () {
Navigator.of(context).pop();
},
),
floatingActionButton: new FloatingActionButton(
elevation: 0.0,
child: new Icon(Icons.add),
onPressed: () {
Navigator.pushNamed(context, PageB.ROUTE).then((onValue) async{
_bloc.getList(context); // call this function once it is back from pageB
setState(() {});
});
}));
}
第一次加载页面A时,它将调用initState,并且列表成功填充到listView。但是,如果单击浮动操作按钮转到页面B,然后单击返回( Navigator.pop(context,true);
),则列表不会填充到listView中。始终处于等待状态(保持显示loading.gif)。
When page A is first loaded, it will call initState, and list is successfully populated to listView. But if I click the floating action button to go to page B, then click back ( Navigator.pop(context, true);
), the list doesn't populated to listView. It always in waiting(Keeps display loading.gif).
推荐答案
这是我解决的方法。
在 didChangeDependencies
中添加听:false
。
@override
void didChangeDependencies() {
_bloc = Provider.of<Bloc>(context, listen: false);
super.didChangeDependencies();
}
这篇关于ConnectionState始终在等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!