Navigator.pop

时间:2019-06-20 15:16:07

标签: flutter dart stream localization bloc

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

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);
      }
    });
  }
}

然后,我正在使用此服务来创建一个遵循当前用户位置的地图(由于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. 我启动了我的应用,位于第1页上。我有SelfUpdatingMap
  2. 我打Navigator.pushNamed(context, '/page-2')
  3. 我打电话给Navigator.pushNamed(context, '/page-3')。我还有另一个SelfUpdatingMap
  4. 我打了两次Navigator.pop(context),我得到了第1页 但是SelfUpdatingMap不再更新了。

甚至不再调用生成器。因此,请问这段代码有什么问题?

谢谢!

1 个答案:

答案 0 :(得分:1)

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

解决方案示例:

import xlwt
wb = xlwt.Workbook() # create empty workbook object
sheet1 = wb.add_sheet('my_sheet_name') # sheet name can not be longer than 32 characters
sheet1 = wb.add_sheet('sheet1')
sheet1.write(0,0, ' Date')
sheet1.write(0,1, 'Name')
sheet1.write(0,2, 'File number')
sheet1.write(0,3, 'Returned/Taken')

for value in range(2):
  i=0
  j=0
  date = input('please enter date')
  sheet1.write(i+1,j,date) # write contents to a cell, marked by row i, column j
  name = input('please enter your name')
  sheet1.write(i+1,j+1,name)
  n_files = input('how many files would you like')
  n_files = int(n_files)
  for v in range(0,n_files):
    f_number = input('please input filenumber')
    i+=1
    sheet1.write(i,j+2,f_number)
    status = input('are you taking the file yes/no')
    sheet1.write(i ,j+3, status)

# no update allowed for content writing?
wb.save('registry3.xls')