Skip to content

Commit 3140991

Browse files
committed
[google_maps_flutter] Fix onMapCreated hanging when tiles fail to load
The map was reported as ready only on the first `tilesloaded` event, which requires every visible tile to finish downloading. A single stalled or failed tile request left `onMapCreated` pending forever. Also report the map as ready on the first `idle` event once the map div has a non-zero size, as google_maps_flutter_web does. Upstream: flutter/packages#13059 (google_maps_flutter_web 0.6.4+2, flutter/flutter#193452)
1 parent cba72ff commit 3140991

2 files changed

Lines changed: 18 additions & 5 deletions

File tree

‎packages/google_maps_flutter/CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* Add support for `onPointOfInterestTap`.
55
* Fix `consumeTapEvents` for circles, polygons, and polylines.
66
* Fix initial map objects being added to the map twice.
7+
* Fix `onMapCreated` never being called when a map tile request stalls or fails.
78
* Update google_maps_flutter_platform_interface to 2.17.0.
89
* Resolve `use_super_parameters` lint.
910
* Omit obvious local variable types.

‎packages/google_maps_flutter/lib/src/google_maps_controller.dart‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,9 @@ class GoogleMapsController {
139139
case MapsJsEventType.boundsChanged:
140140
await _onBoundsChanged();
141141
case MapsJsEventType.idle:
142-
_onIdle();
142+
await _onIdle();
143143
case MapsJsEventType.tilesLoaded:
144-
_onTilesloaded();
144+
_onMapReady();
145145
case MapsJsEventType.click:
146146
_onClick(event.message!);
147147
case MapsJsEventType.longPress:
@@ -215,14 +215,26 @@ class GoogleMapsController {
215215
}
216216
}
217217

218-
void _onIdle() {
218+
Future<void> _onIdle() async {
219219
_mapIsMoving = false;
220220
_streamController.add(CameraIdleEvent(_mapId));
221+
// NOTE: `tilesloaded` never fires if a tile request stalls or fails.
222+
// See https://github.com/flutter/flutter/issues/193452.
223+
if (!_isFirst && await _isMapLaidOut()) {
224+
_onMapReady();
225+
}
226+
}
227+
228+
Future<bool> _isMapLaidOut() async {
229+
final Object value = await _bridge.runJavaScriptReturningResult(
230+
'map.getDiv().offsetWidth > 0 && map.getDiv().offsetHeight > 0',
231+
);
232+
return value.toString() == 'true';
221233
}
222234

223-
void _onTilesloaded() {
235+
void _onMapReady() {
224236
try {
225-
if (_isFirst) {
237+
if (_isFirst || _streamController.isClosed) {
226238
return;
227239
}
228240
_streamController.add(MapReadyEvent(_mapId));

0 commit comments

Comments
 (0)