I am trying to move the center of a map whenever I retrieve the user's location.
I have the following code to do that:
void initState() {
super.initState();
mapController = new MapController();
try {
new Location().getLocation.then((Map<String, double> loc) {
print(loc);
print("Map ready: " + mapController.ready.toString());
setState(
() => location = new LatLng(loc["latitude"], loc["longitude"]));
mapController.move(location, zoom); // Location and zoom are not null
})/*.catchError((e) => print(e))*/;
} catch (e) {
print(e);
showDialog(
context: context,
builder: (BuildContext context) {
return new Center(
child: new Text("We couldn't get your location!"),
);
});
}
}
Doing this, I'm getting the following error:
I/flutter (21207): {altitude: 3.40031719, latitude: 33.985311, accuracy: 11.723999977111816, speed_accuracy: 0.0, speed: 0.0, longitude: -118.2043953}
E/flutter (21207): [ERROR:topaz/lib/tonic/logging/dart_error.cc(16)] Unhandled exception:
E/flutter (21207): NoSuchMethodError: The method '/' was called on null.
E/flutter (21207): Receiver: null
E/flutter (21207): Tried calling: /(2.0)
E/flutter (21207): #0 Object.noSuchMethod (dart:core/runtime/libobject_patch.dart:46:5)
E/flutter (21207): #1 MapState.getNewPixelOrigin (package:flutter_map/src/map/map.dart:187:30)
E/flutter (21207): #2 MapState.move (package:flutter_map/src/map/map.dart:85:20)
E/flutter (21207): #3 MapControllerImpl.move (package:flutter_map/src/map/map.dart:23:12)
E/flutter (21207): #4 MapViewState.initState.<anonymous closure> (file:///C:/Users/bramv/Documents/Yes%20Theory/map_test/lib/main.dart:390:23)
E/flutter (21207): #5 _RootZone.runUnary (dart:async/zone.dart:1381:54)
E/flutter (21207): #6 _FutureListener.handleValue (dart:async/future_impl.dart:129:18)
E/flutter (21207): #7 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:638:45)
E/flutter (21207): #8 Future._propagateToListeners (dart:async/future_impl.dart:667:32)
E/flutter (21207): #9 Future._complete (dart:async/future_impl.dart:472:7)
E/flutter (21207): #10 _SyncCompleter.complete (dart:async/future_impl.dart:51:12)
E/flutter (21207): #11 _AsyncAwaitCompleter.complete (dart:async/runtime/libasync_patch.dart:28:18)
E/flutter (21207): #12 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart)
E/flutter (21207): <asynchronous suspension>
E/flutter (21207): #13 Location.getLocation (package:location/location.dart:12:8)
E/flutter (21207): #14 MapViewState.initState (file:///C:/Users/bramv/Documents/Yes%20Theory/map_test/lib/main.dart:386:22)
E/flutter (21207): #15 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3764:58)
E/flutter (21207): #16 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3630:5)
E/flutter (21207): #17 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2920:14)
E/flutter (21207): #18 MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4772:32)
E/flutter (21207): #19 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2920:14)
E/flutter (21207): #20 Element.updateChild (package:flutter/src/widgets/framework.dart:2723:12)
E/flutter (21207): #21 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3666:16)
E/flutter (21207): #22 Element.rebuild (package:flutter/src/widgets/framework.dart:3508:5)
E/flutter (21207): #23 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3635:5)
E/flutter (21207): #24 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3630:5)
E/flutter (21207): #25 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2920:14)
E/flutter (21207): #26 Element.updateChild (package:flutter/src/widgets/framework.dart:2723:12)
E/flutter (21207): #27 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4667:14)
E/flutter (21207): #28 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2920:14)
E/flutter (21207): #29 Element.updateChild (package:flutter/src/widgets/framework.dart:2723:12)
E/flutter (21207): #30 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3666:16)
E/flutter (21207): #31 Element.rebuild (package:flutter/src/widgets/framework.dart:3508:5)
E/flutter (21207): #32 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3635:5)
E/flutter (21207): #33 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3630:5)
E/flutter (21207): #34 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2920:14)
E/flutter (21207): #35 Element.updateChild (package:flutter/src/widgets/framework.dart:2723:12)
E/flutter (21207): #36 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3666:16)
E/flutter (21207): #37 Element.rebuild (package:flutter/src/widgets/framework.dart:3508:5)
E/flutter (21207): #38 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3635:5)
E/flutter (21207): #39 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3630:5)
E/flutter (21207): #40 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2920:14)
E/flutter (21207): #41 Element.updateChild (package:flutter/src/widgets/framework.dart:2723:12)
E/flutter (21207): #42 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3666:16)
E/flutter (21207): #43 Element.rebuild (package:flutter/src/widgets/framework.dart:3508:5)
E/flutter (21207): #44 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3635:5)
E/flutter (21207): #45 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3630:5)
E/flutter (21207): #46 ParentDataElement.mount
I also checked whether the map controller is ready and it is.
I used the following code to create the map:
new FlutterMap(
mapController: mapController,
options: new MapOptions(
center: fallbackLocation,
zoom: zoom,
),
layers: [
new TileLayerOptions(
urlTemplate: "https://api.tiles.mapbox.com/v4/"
"{id}/{z}/{x}/{y}@2x.png?access_token=...",
additionalOptions: {
'accessToken':
'<...>',
'id': 'mapbox.streets',
},
),
new MarkerLayerOptions(
markers: location == null
? []
: [
new Marker(
width: 40.0,
height: 40.0,
point: location,
builder: (ctx) => new Container(
child: new Icon(
Icons.add_location,
color: Colors.redAccent,
size: 40.0,
),
),
)
]),
]),
Any reason why this would fail?
I am trying to move the center of a map whenever I retrieve the user's location.
I have the following code to do that:
Doing this, I'm getting the following error:
I also checked whether the map controller is ready and it is.
I used the following code to create the map:
Any reason why this would fail?