gpt4 book ai didi

dart - Streambuilder 没有收到一些快照数据

转载 作者:IT老高 更新时间:2023-10-28 12:40:58 27 4
gpt4 key购买 nike

我只是在测试来自 GPS 的一些流。我可以直接插入 gps 流,但我想暂时将其分开。所以我可以将 StreamBuilder 与我自己创建的流一起使用。

这一切似乎都在工作,但 Streambuilder 似乎“错过”了某些快照,这让我感到困惑。难道不应该保证从流中接收所有数据(如果它不是广播)?还是我错误地使用了流?

如果您查看添加/接收的数据,我可以看到计数 5 和 7 都已添加,但从未收到。如果我只是在没有 StreamBuilder 的情况下“收听”数据,所有数据似乎都会出现。

Streambuilder 代码:

Widget build(BuildContext context) {

final ApplicationBloc bloc = BlocProvider.of<ApplicationBloc>(context);

return StreamBuilder<Map<String, dynamic>>(
stream: bloc.gps.stream,
builder: (BuildContext context, AsyncSnapshot<Map<String, dynamic>> snapshot){
if( snapshot.hasData ) {
print("Receiving ${snapshot.data['count']}");
return Text("${snapshot.data['timestamp']}\n${snapshot.data['latitude']},${snapshot.data['longitude']}\n",
style: Theme.of(context).textTheme.display1,);
} else {
return Text('waiting for GPS...', style: Theme.of(context).textTheme.display1,);
}
}
);

}

添加到流代码:

var locationOptions = LocationOptions(accuracy: LocationAccuracy.bestForNavigation, distanceFilter: 0, timeInterval: 1, forceAndroidLocationManager: false);

final Geolocator geoLocator = new Geolocator();
geoLocator.getPositionStream( locationOptions ).listen(
(Position position) {
_count++;
Map<String, dynamic> listenerCurrentLocation = {
'latitude' : position.latitude,
'longitude' : position.longitude,
'speed' : position.speed,
'accuracy' : position.accuracy,
'altitude' : position.altitude,
'timestamp' : position.timestamp,
'count' : _count,
};
print( "Adding: $listenerCurrentLocation");
gpsController.sink.add( listenerCurrentLocation );
});

运行控制台的输出

I/flutter (16345): Receiving 2
I/flutter (16345): Adding: {latitude: 50.829798333333336, longitude: -0.18484833333333334, speed: 0.0, accuracy: 20.0, altitude: 11.7, timestamp: 2019-01-13 14:21:02.000Z, count: 3}
I/flutter (16345): Receiving 3
I/flutter (16345): Adding: {latitude: 50.829798333333336, longitude: -0.18484833333333334, speed: 0.0, accuracy: 20.0, altitude: 11.7, timestamp: 2019-01-13 14:21:03.000Z, count: 4}
I/flutter (16345): Receiving 4
I/flutter (16345): Adding: {latitude: 50.829798333333336, longitude: -0.18484833333333334, speed: 0.0, accuracy: 20.0, altitude: 11.7, timestamp: 2019-01-13 14:21:04.000Z, count: 5}
I/flutter (16345): Adding: {latitude: 50.829798333333336, longitude: -0.18484833333333334, speed: 0.0, accuracy: 20.0, altitude: 0.0, timestamp: 2019-01-13 14:21:04.000Z, count: 6}
I/flutter (16345): Receiving 6
I/flutter (16345): Adding: {latitude: 50.829798333333336, longitude: -0.18484833333333334, speed: 0.0, accuracy: 20.0, altitude: 11.7, timestamp: 2019-01-13 14:21:05.000Z, count: 7}
I/flutter (16345): Adding: {latitude: 50.829798333333336, longitude: -0.18484833333333334, speed: 0.0, accuracy: 20.0, altitude: 0.0, timestamp: 2019-01-13 14:21:05.000Z, count: 8}
I/flutter (16345): Receiving 8

最佳答案

这是预期的行为。

与使用 stream.listen 不同,StreamBuilder 仅在请求新帧时调用其构建器。

这很好,因为StreamBuilder 应该 用于构建布局,在这种情况下,如果错过某个值也没关系。

如果不希望出现这种行为,请考虑改为手动收听:

Stream stream;
StreamSubscription sub;

initState() {
super.initState();
sub = stream.listen((value) {
// do something with `value`
});
}


dispose() {
sub.cancel();
super.dispose();
}

关于dart - Streambuilder 没有收到一些快照数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54169848/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com