本文介绍了Streambuilder未接收到某些快照数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am just testing some streams from a GPS. I could plug in the gps stream directly, but I want to separate this for the moment. So I can using a StreamBuilder with my own created stream.

这一切似乎都有效,但是Streambuilder似乎丢失"了某些快照,这让我感到困惑.是否应该保证不能从流中接收所有数据(如果不是广播)?还是我不正确地使用流?

This all seems to be working, but the Streambuilder seems to 'miss' certain snapshots, which has me confused. Shouldn't it be guaranteed to receive all data from the stream (if it's not a broadcast)? Or am I using streams incorrectly ?

如果您查看添加/接收的数据,我会看到计数5&都添加了7个,但从未收到.如果我只是在不使用StreamBuilder的情况下监听"数据,则所有数据似乎都会出现.

If you look at the data added/received, I can see count 5 & 7 were both added, but never received. If I just 'listen' the data without the StreamBuilder, all of the data seems to appear.

Streambuilder代码:

Streambuilder code:

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 仅在请求新框架时才调用其生成器.

As opposed to using stream.listen, StreamBuilder calls its builder only when a new frame is requested.

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

It is fine because StreamBuilder should be used only to build the layout, in which case it doesn't matter if a value is missed.

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

If this behavior is undesired, consider switching to a manual listening instead:

Stream stream;
StreamSubscription sub;

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


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

这篇关于Streambuilder未接收到某些快照数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 19:34