flutter_map icon indicating copy to clipboard operation
flutter_map copied to clipboard

mapController.bounds is null till map once fully rendered

Open yostrokon opened this issue 8 years ago • 2 comments

On the first start have an exception. After map fully rendered and on the next load - everything is OK. What should i do to fix this? Or somehow to delay loading markes till map is rendered?

E/flutter (28144): [ERROR:flutter/shell/common/shell.cc(181)] Dart Error: Unhandled exception: E/flutter (28144): NoSuchMethodError: The getter 'bounds' was called on null. E/flutter (28144): Receiver: null E/flutter (28144): Tried calling: bounds E/flutter (28144): #0 Object.noSuchMethod (dart:core/runtime/libobject_patch.dart:50:5) E/flutter (28144): #1 MapControllerImpl.bounds (package:flutter_map/src/map/map.dart:39:37) E/flutter (28144): #2 _ProjectPageState._loadNavPoints (package:maptry/project.dart:109:33)

Map<String, NavPoint> _navPoints = Map();

Future _loadNavPoints() async{ List _tPoints = List();

_oldBounds = _mapController.bounds; // error as described ^^^ till map once fully rendered

// get navigational points from the backend within _oldBounds frame

  return _tPoints;

}

List _loadMarkers(){ List _markers = new List();

_navPoints.forEach((key, navPoint)=>_markers.add(_buildMarker(navPoint))); return _markers; }

Marker _buildMarker(NavPoint navPoint){ return Marker( width: 45.0, height: 45.0, point: LatLng( navPoint.latitude, navPoint.longitude), builder: (ctx) => Container( decoration: BoxDecoration( borderRadius: BorderRadius .circular(45.0), color: Theme .of(context) .primaryColor .withOpacity(0.1), border: Border.all( width: 1.0) ), child: Center( child: Text(navPoint.name), ) ),

); }

@override Widget build(BuildContext context) { return FlutterMap( mapController: _mapController, options: MapOptions( center: LatLng(_currentLocation.latitude, _currentLocation.longitude), zoom: 13.0, minZoom: 2.0, maxZoom: 18.0, onPositionChanged: null, ), layers: [ TileLayerOptions( urlTemplate: 'https://cartodb-basemaps-{s}.global.ssl.fastly.net/rastertiles/voyager/{z}/{x}/{y}@2x.png.png', subdomains: ['a', 'b', 'c', 'd'], ), MarkerLayerOptions( markers: _loadMarkers() ), ], ); }

yostrokon avatar Nov 03 '18 11:11 yostrokon

i don't know may be some of the map property like "onready" that will execute user defined callback when map is rendered would be most suitable in this case

yostrokon avatar Nov 05 '18 04:11 yostrokon

Actually did initial load using onPositionChanged, later will think how to awoid backend spam with a lot of requests during pan

yostrokon avatar Nov 05 '18 05:11 yostrokon