我处于错误状态:运行下面的代码时,我已经在StreamView中收听了流,我在Tabview中调用我的流可以很好地运行,但是当我在Tab视图中玩耍时,表明我已经侦听了Stream。不明白是怎么回事。
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));
}
}
这是我的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",
),
),
);
请有人帮我吗?
答案 0 :(得分:0)
试试这个:
final StreamController _counter = StreamController.broadcast();