Flutter 错误状态:流已被侦听
我的状态不好:在运行下面的代码时,流已经被监听。我在选项卡视图中调用流。运行正常,但当我使用选项卡视图时,它显示流已经被监听。我不明白是怎么回事Flutter 错误状态:流已被侦听,flutter,dart,stream-builder,Flutter,Dart,Stream Builder,我的状态不好:在运行下面的代码时,流已经被监听。我在选项卡视图中调用流。运行正常,但当我使用选项卡视图时,它显示流已经被监听。我不明白是怎么回事 class DataServiceColi { static Future loadJson(String url) async { await Future.delayed(Duration(seconds: 5)); // Await the http get response, then decode t
class DataServiceColi {
static Future loadJson(String url) async {
await Future.delayed(Duration(seconds: 5));
// Await the http get response, then decode t he json-formatted response.
http.Response response = await http.get(url);
if (response.statusCode == 200) {
String content = response.body;
collectionColi = json.decode(content);
print('$collectionColi');
return collectionColi;
} else {
print('Request failed with status: ${response.statusCode}.');
}
}
}
class DataManagerColis{
final StreamController<int> _counter = StreamController<int>();
final StreamController<List> streamController = new BehaviorSubject();
Stream<List> get counterlist => streamController.stream;
Stream<List> get colistView async*{
yield await DataServiceColi.loadJson(colis);
}
@override
void dispose() {
print('Disposed Statistics Bloc');
streamController.close();
}
DataManagerColis(){
colistView.listen((list) => streamController.add(list));
}
}
classdataservicecoli{
静态未来加载JSON(字符串url)异步{
等待未来。延迟(持续时间(秒:5));
//等待http get响应,然后解码json格式的响应。
http.Response-Response=等待http.get(url);
如果(response.statusCode==200){
字符串内容=response.body;
collectionColi=json.decode(内容);
打印(“$collectionColi”);
回收大肠杆菌;
}否则{
打印('请求失败,状态:${response.statusCode}');
}
}
}
类DataManagerColis{
最终StreamController _计数器=StreamController();
最终StreamController StreamController=新行为主体();
Stream get counterlist=>streamController.Stream;
流获取colistView异步*{
产生wait-DataServiceColi.loadJson(colis);
}
@凌驾
无效处置(){
打印(“已处理的统计数据集团”);
streamController.close();
}
DataManagerColis(){
colistView.listen((list)=>streamController.add(list));
}
}
此my UI小部件:
@override
Widget build(BuildContext context) {
return DefaultTabController(
length: 3,
child: Scaffold(
backgroundColor: Color.fromRGBO(58, 66, 86, 1.0),
appBar: topAppBar,
body:TabBarView(children:[
StreamBuilder<List>(
stream: manager.colistView,
builder:(BuildContext context, AsyncSnapshot<List<dynamic>> snapshot){
switch (snapshot.connectionState) {
case ConnectionState.none: return Text('Select lot');
case ConnectionState.waiting:return Center(child: CircularProgressIndicator(backgroundColor:Colors.amberAccent,));
case ConnectionState.active: return Center(child: CircularProgressIndicator(backgroundColor: Colors.cyanAccent,));
case ConnectionState.done:
return ListView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: true,
itemCount: snapshot.data?.length ?? 0,
//separatorBuilder: (context, index) => Divider(),
itemBuilder: (BuildContext context, int index) {
print(index);
return Card(
elevation: 8.0,
margin: new EdgeInsets.symmetric(horizontal: 10.0, vertical: 6.0),
child: Container(
decoration: BoxDecoration(color: Color.fromRGBO(64, 75, 96, .9)),
child: ListTile(
onTap: (){
if(snapshot.data[index]["itinéraires"].length == 0 ){
showDialog(
context: context,
builder: (_) => NetworkGiffyDialog(
buttonOkColor: Color.fromRGBO(64, 75, 96, .9),
key: keys[1],
image: Image.network(
"https://raw.githubusercontent.com/Shashank020519.gif"
fit: BoxFit.cover,
),
entryAnimation: EntryAnimation.DEFAULT,
title: Text(
'Pas encore defini',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 22.0, fontWeight: FontWeight.w600),
),
description: Text(
"nothing"
textAlign: TextAlign.center,
),
onOkButtonPressed: () {},
));
}else{
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => TimelinePage(title: 'suivi ${snapshot.data[index]["libelle_coli"]}',
trajet:snapshot.data[index]["itinéraires"],
)
));
}
},
contentPadding: EdgeInsets.symmetric(horizontal: 20.0, vertical: 10.0),
leading: Container(
padding: EdgeInsets.only(right: 12.0),
decoration: new BoxDecoration(
border: new Border(
right: new BorderSide(width: 1.0, color: Colors.white24))),
child: Icon(Icons.autorenew, color: Colors.white),
),
title: Text(
"${snapshot.data[index]["libelle_coli"]}",
style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
),
// subtitle: Text("Intermediate", style: TextStyle(color: Colors.white)),
subtitle:staut(index,snapshot.data),
trailing:
Icon(Icons.keyboard_arrow_right, color: Colors.white, size: 30.0)),
),
);
},
);
}
//return null;
}
),
new Container(
child: Center(
child: Text("History"),
),
),
MessagingWidget()
]),
floatingActionButton: FloatingActionButton(
onPressed: () {
// Add your onPressed code here!
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => Page_init()
));
},
child: Icon(Icons.add, color:Colors.amber,),
backgroundColor: Color.fromRGBO(58, 66, 86, 1.0),
tooltip: "register something",
),
),
);
@覆盖
小部件构建(构建上下文){
返回DefaultTabController(
长度:3,
孩子:脚手架(
背景颜色:颜色。来自RGBO(58,66,86,1.0),
appBar:topAppBar,
正文:选项卡视图(子项:[
StreamBuilder(
流:manager.colistView,
生成器:(BuildContext上下文,异步快照){
交换机(快照.连接状态){
case ConnectionState.none:返回文本('Select lot');
案例连接状态。等待:返回中心(子项:循环进程指示器(背景颜色:Colors.amberAccent,);
case ConnectionState.active:返回中心(子级:CircularProgressIndicator(背景色:Colors.cyanAccent,);
案例连接状态。完成:
返回ListView.builder(
滚动方向:轴垂直,
收缩膜:对,
itemCount:snapshot.data?长度为0,
//separatorBuilder:(上下文,索引)=>Divider(),
itemBuilder:(构建上下文,int索引){
打印(索引);
回程卡(
标高:8.0,
边距:新边集。对称(水平:10.0,垂直:6.0),
子:容器(
装饰:盒子装饰(颜色:color.fromRGBO(64,75,96,9)),
孩子:ListTile(
onTap:(){
if(snapshot.data[index][“itinéraires”].length==0){
显示对话框(
上下文:上下文,
生成器:()=>NetworkGiffyDialog(
buttonOkColor:Color.fromRGBO(64,75,96,9),
键:键[1],
图片:image.network(
"https://raw.githubusercontent.com/Shashank020519.gif"
适合:BoxFit.cover,
),
entryAnimation:entryAnimation.DEFAULT,
标题:正文(
“Pas encore defini”,
textAlign:textAlign.center,
样式:TextStyle(
fontSize:22.0,fontWeight:fontWeight.w600),
),
说明:文本(
“没什么”
textAlign:textAlign.center,
),
onOkButtonPressed:(){},
));
}否则{
导航器。推(
上下文
材料路线(
生成器:(上下文)=>TimelinePage(标题:'suivi${snapshot.data[index][“libelle_coli”]}),
trajet:snapshot.data[索引][“itinéraires”],
)
));
}
},
内容填充:边集。对称(水平:20.0,垂直:10.0),
领先:集装箱(
填充:仅限边缘设置(右侧:12.0),
装饰:新盒子装饰(
边界:新边界(
右:新边框(宽度:1.0,颜色:Colors.white24)),
子项:图标(Icons.autorenew,颜色:Colors.white),
),
标题:正文(
“${snapshot.data[index][“libelle_coli”]}”,
样式:TextStyle(颜色:Colors.white,fontwweight:fontwweight.bold),
),
//字幕:文本(“中间”,样式:文本样式(颜色:Colors.white)),
字幕:staut(索引、快照、数据),
尾随:
图标(Icons.keyboard_arrow_right,颜色:Colors.white,大小:30.0)),
),
);
},
);
}
//返回null;