gpt4 book ai didi

flutter - Navigator.pop之后,StreamBuilder不会重建

转载 作者:行者123 更新时间:2023-12-03 03:01:47 24 4
gpt4 key购买 nike

我有一个跟踪当前用户位置的简单服务:

class LocationService {
LatLng _lastLocation;
Location location = Location();

StreamController<LatLng> _locationController = StreamController<LatLng>();
Stream<LatLng> get locationStream => _locationController.stream;

LocationService() {
location.onLocationChanged().listen((locationData) {
LatLng location = LatLng(locationData.latitude, locationData.longitude);
if(_lastLocation == null || _lastLocation != location) {
_lastLocation = location;
_locationController.add(location);
}
});
}
}

然后,我正在使用此服务创建一个遵循当前用户位置的 map (由于 flutter_map):

class SelfUpdatingMap extends StatelessWidget {
final Icon currentPositionIcon;

final MapController _controller = MapController();

SelfUpdatingMap({
this.currentPositionIcon,
});

@override
Widget build(BuildContext context) => StreamBuilder<LatLng>(
stream: LocationService().locationStream,
builder: (context, asyncSnapshot) {
if (asyncSnapshot.hasError || asyncSnapshot.data == null) {
return Text('Loading...');
}

try {
_controller?.move(asyncSnapshot.data, 18);
} catch (ignored) {}
return _createMapWidget(context, asyncSnapshot.data);
},
);

Widget _createMapWidget(BuildContext context, LatLng location) => FlutterMap(
options: MapOptions(
center: location,
zoom: 18,
),
layers: [
TileLayerOptions(
urlTemplate: 'https://{s}.basemaps.cartocdn.com/rastertiles/voyager/{z}/{x}/{y}.png', // https://a.tile.openstreetmap.fr/osmfr/{z}/{x}/{y}.png is good too.
subdomains: ['a', 'b', 'c'],
),
MarkerLayerOptions(
markers: [
Marker(
width: 40,
height: 40,
point: location,
builder: (contact) => currentPositionIcon,
),
]
),
],
mapController: _controller,
);
}

然后,在两个地方使用 SelfUpdating小部件:
  • 第1页,第2页的祖先。
  • 在第3页中,是第2页的后继。

  • 所以这是情况:
  • 我启动了我的应用程序,位于页面1上。我有SelfUpdatingMap
  • 我叫Navigator.pushNamed(context, '/page-2')
  • 我称为Navigator.pushNamed(context, '/page-3')。我还有另一个SelfUpdatingMap
  • 我调用了两次Navigator.pop(context),得到了页面1 但是SelfUpdatingMap不再更新。

  • 生成器甚至不再被调用。所以,请问这段代码有什么问题?

    谢谢 !

    最佳答案

    当您按下并弹出页面后,构建方法不会重新启动。
    我在FlutterBluetoothSerial.instance.onStateChanged()流中发现了同样的问题,而我发现的解决方案是将流添加到本地静态最终变量中并使用它,而不是每次都使用原始方法调用(仅当该流是我认为的广播流)。

    解决方案示例:

    class ExampleClass {
    static final Stream<LatLng> locationStream = LocationService().locationStream;
    }

    class SelfUpdatingMap extends StatelessWidget {
    ...
    @override
    Widget build(BuildContext context) => StreamBuilder<LatLng>(
    stream: ExampleClass.locationStream,
    builder: (context, asyncSnapshot) {
    if (asyncSnapshot.hasError || asyncSnapshot.data == null) {
    return Text('Loading...');
    }
    try {
    _controller?.move(asyncSnapshot.data, 18);
    } catch (ignored) {}
    return _createMapWidget(context, asyncSnapshot.data);
    },
    );
    ...
    }

    class Page3Widget extends StatelessWidget {
    ...
    @override
    Widget build(BuildContext context) => StreamBuilder<LatLng>(
    stream: ExampleClass.locationStream,
    builder: (context, asyncSnapshot) {
    //do something
    },
    );
    ...
    }

    关于flutter - Navigator.pop之后,StreamBuilder不会重建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56689093/

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