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

我只是在测试来自 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();
}

https://stackoverflow.com/questions/54169848/

相关文章:

user-interface - 如何在 flutter 中获得相对于屏幕尺寸的小部件尺寸?

regex - Flutter 无法解析正则表达式

dart - flutter/Dart 异步不等待

flutter - 因为sdk的flutter_test的每个版本都依赖... sdk的flutte

android - Flutter - 多选 ListView

dart - 从 LatLng 列表计算总距离

dart - Flutter - 修复抽屉页眉

dart - Flutter 有数据绑定(bind)吗?

flutter - 如何更改 CupertinoDatePicker 显示语言?

intellij-idea - 如何在 IntelliJ 中自动完成/导入 Flutter 类?