我正在尝试使用StreamBuilder从Stream中读取一些数据,但是snapshot.data仅返回Stream中的最后一项。在这种情况下,我只会得到“ f”。我想获得“ a”,“ b”,“ c”,“ d”,“ e”,“ f”。我在做什么错了?
class ProgressWidget extends StatefulWidget {
//ProgressWidget({this.stream}) : super();
//final Stream<String> stream;
@override
_ProgressWidgetState createState() => _ProgressWidgetState();
}
class _ProgressWidgetState extends State<ProgressWidget> {
int _progressCount = 0;
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return StreamBuilder(
stream: Stream.fromIterable(['a', 'b', 'c', 'd', 'e', 'f']), // test stream
builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
_progressCount++;
if (snapshot.hasData) {
print(snapshot.data);
}
return snapshot.hasData
? new Text('Progress $_progressCount ' + snapshot.data)
: new Text('Loading ... ');
});
}
}
答案 0 :(得分:2)
像这样创建您的流:
Stream<String> listStream(Duration interval, List<String> list) async* {
int i = 0;
while (i<list.length) {
await Future.delayed(interval);
yield list[i];
i++;
}
}
最终代码:
class ProgressWidget extends StatefulWidget {
//ProgressWidget({this.stream}) : super();
//final Stream<String> stream;
@override
_ProgressWidgetState createState() => _ProgressWidgetState();
}
class _ProgressWidgetState extends State<ProgressWidget> {
int _progressCount = 0;
String str = '';
@override
void initState() {
super.initState();
}
Stream<String> listStream(Duration interval, List<String> list) async* {
int i = 0;
while (i<list.length) {
await Future.delayed(interval);
yield list[i];
i++;
}
}
@override
Widget build(BuildContext context) {
return StreamBuilder<String>(
stream:listStream(Duration(milliseconds:300), ['a', 'b', 'c', 'd', 'e', 'f']),
builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
_progressCount++;
if (snapshot.hasData) {
print(snapshot.data);
str += snapshot.data;
}
return snapshot.hasData
? new Text('Progress $_progressCount ' + str)
: new Text('Loading ... ');
});
}
}
答案 1 :(得分:1)
尝试一下:
class _ProgressWidgetState extends State<ProgressWidget> {
int _progressCount = 0;
Stream<String> _stream;
List<String> _source = ['a', 'b', 'c', 'd', 'e', 'f'];
@override
void initState() {
super.initState();
_stream = Stream.periodic(Duration(seconds: 1), (i) => _source[i]).take(_source.length);
}
@override
Widget build(BuildContext context) {
return StreamBuilder(
stream: _stream, // test stream
builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
_progressCount++;
return snapshot.hasData ? new Text('Progress $_progressCount ' + snapshot.data) : new Text('Loading ... ');
},
);
}
}