Closed jiazeh closed 6 months ago
Cannot repoduce. I think you are facing this error when doing hot reload. The error message show that you subscript the stream twice. A non-boardcast stream can only be subscript once. You should recreate the stream each time if you unsubscript. If you remove the CurrentLocationLayer by hot reload, your _alignPositionStreamController.stream is already disposed. It cannot be used again. If you really need to reuse the stream, consider use a boardcast stream.
StreamController.broadcast();
Thank you so much for your reply. Here is a record of how the error occurred and the situation after the problem was solved...
_P.S. The symbol "!!!" is represents the number of calls to _subscriptFollowCurrentLocationStream
._
Cause of the problem
The result after switching to the StreamController.broadcast()
Code to reproduce
class _CenterFabExampleState extends State<CenterFabExample> {
late AlignOnUpdate _alignPositionOnUpdate;
late final StreamController<double?> _oldPositionStreamController;
// late final StreamController<double?> _newPositionStreamController;
bool _enableLocService = false;
bool _enableMapRoadmap = false;
@override
void initState() {
_alignPositionOnUpdate = AlignOnUpdate.always;
_oldPositionStreamController = StreamController<double?>();
// _newPositionStreamController = StreamController<double?>.broadcast();
super.initState();
}
@override
void dispose() {
_oldPositionStreamController.close();
// _newPositionStreamController.close();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Center FAB Example'),
),
body: FlutterMap(
options: MapOptions(
initialCenter: const LatLng(0, 0),
initialZoom: 1,
minZoom: 0,
maxZoom: 19,
onPositionChanged: (MapPosition position, bool hasGesture) {
if (hasGesture && _alignPositionOnUpdate != AlignOnUpdate.never) {
setState(
() => _alignPositionOnUpdate = AlignOnUpdate.never,
);
}
},
),
children: [
TileLayer(
urlTemplate: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
userAgentPackageName:
'net.tlserver6y.flutter_map_location_marker.example',
maxZoom: 19,
),
// The layer can be displayed normally if placed here, but it will be obscured.
// if (_enableLocService)
// CurrentLocationLayer(
// alignPositionStream: _oldPositionStreamController.stream,
// // alignPositionStream: _newPositionStreamController.stream,
// alignPositionOnUpdate: _alignPositionOnUpdate,
// ),
if (_enableMapRoadmap)
TileLayer(
urlTemplate:
'https://s3.amazonaws.com/te512.safecast.org/{z}/{x}/{y}.png',
maxZoom: 16,
),
// An error will occur if the layer is placed here.
if (_enableLocService)
CurrentLocationLayer(
alignPositionStream: _oldPositionStreamController.stream,
// alignPositionStream: _newPositionStreamController.stream,
alignPositionOnUpdate: _alignPositionOnUpdate,
),
],
),
floatingActionButton: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
FloatingActionButton(
heroTag: 'toggle',
onPressed: () => setState(() {
_enableMapRoadmap = !_enableMapRoadmap;
}),
child: const Icon(Icons.legend_toggle),
),
const SizedBox(height: 12),
FloatingActionButton(
heroTag: 'location',
onPressed: () {
setState(() {
_enableLocService = true;
_alignPositionOnUpdate = AlignOnUpdate.always;
});
_oldPositionStreamController.add(14);
// _newPositionStreamController.add(14);
},
child: const Icon(
Icons.my_location,
color: Colors.white,
),
),
],
),
);
}
}
Describe the bug When the
CurrentLocationLayer
is overlaid on the dynamic layer, an error ofUnhandled Exception: Bad state: Stream has not been Listened.
will be thrown.Error Message
To Reproduce
Trigger wrong behavior After location, when the following code is clicked.
Screenshots
Smartphone Platforms:
Additional context This problem only occurs in the following package versions: 8.0.0, 8.0.1, 8.0.2, 8.0.3