Streambuilder没有收到一些快照数据

时间:2019-01-13 14:30:16

标签: dart flutter

我正在测试来自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

1 个答案:

答案 0 :(得分:3)

这是预期的行为。

与使用stream.listen相比,StreamBuilder仅在请求新框架时才调用其生成器。

这很好,因为StreamBuilder仅用于 来构建布局,在这种情况下,是否缺少值都无关紧要。

如果不希望出现这种情况,请考虑改用人工聆听:

Stream stream;
StreamSubscription sub;

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


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