'The getter 'latitude' was called on null. E/flutter ( 9972): Receiver: null E/flutter ( 9972): Tried calling: latitude
I am trying to show latitude and longitude on a map. I am successfully getting latitude and longitude from previous page and pass to my map page, but i always get error on map screen. Latitude and longitude not works.
[ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: NoSuchMethodError: The getter 'latitude' was called on null. E/flutter ( 9972): Receiver: null E/flutter ( 9972): Tried calling: latitude
import 'package:flutter/cupertino.dart';
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:geocoding/geocoding.dart';
import 'package:geolocator/geolocator.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
class MapsInfo extends StatefulWidget {
String lati, longi, title;
int myId;
MapsInfo({Key key, this.title, this.myId, this.lati, this.longi})
: super(key: key);
@override
_MapsInfoState createState() => _MapsInfoState();
}
class _MapsInfoState extends State<MapsInfo> {
Completer<GoogleMapController> controller1;
//static LatLng _center = LatLng(-15.4630239974464, 28.363397732282127);
static LatLng _initialPosition;
final Set<Marker> _markers = {};
static LatLng _lastMapPosition = _initialPosition;
@override
void initState() {
super.initState();
if (widget.myId == null) {
_getUserLocation();
} else {
_initialPosition =
LatLng(double.parse(widget.lati), double.parse(widget.longi));
}
}
void _getUserLocation() async {
Position position = await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.high);
List<Placemark> placemark =
await placemarkFromCoordinates(position.latitude, position.longitude);
setState(() {
_initialPosition = LatLng(position.latitude, position.longitude);
print('${placemark[0].name}');
});
}
_onMapCreated(GoogleMapController controller) {
setState(() {
controller1.complete(controller);
});
}
MapType _currentMapType = MapType.normal;
void _onMapTypeButtonPressed() {
setState(() {
_currentMapType = _currentMapType == MapType.normal
? MapType.satellite
: MapType.normal;
});
}
_onCameraMove(CameraPosition position) {
_lastMapPosition = position.target;
}
_onAddMarkerButtonPressed() {
setState(() {
_markers.add(Marker(
markerId: MarkerId(_lastMapPosition.toString()),
position: _lastMapPosition,
infoWindow: InfoWindow(
title: "Pizza Parlour",
snippet: "This is a snippet",
onTap: () {}),
onTap: () {},
icon: BitmapDescriptor.defaultMarker));
});
}
Widget mapButton(Function function, Icon icon, Color color) {
return RawMaterialButton(
onPressed: function,
child: icon,
shape: new CircleBorder(),
elevation: 2.0,
fillColor: color,
padding: const EdgeInsets.all(7.0),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: _initialPosition == null
? Container(
child: Center(
child: Text(
'loading map..',
style: TextStyle(
fontFamily: 'Avenir-Medium', color: Colors.grey[400]),
),
),
)
: Container(
margin: EdgeInsets.fromLTRB(0.0, 60.0, 0.0, 0.0),
child: Stack(children: <Widget>[
GoogleMap(
markers: _markers,
mapType: _currentMapType,
initialCameraPosition: CameraPosition(
target: _initialPosition,
zoom: 14.4746,
),
onMapCreated: _onMapCreated,
zoomGesturesEnabled: true,
onCameraMove: _onCameraMove,
myLocationEnabled: true,
compassEnabled: true,
myLocationButtonEnabled: true,
),
Align(
alignment: Alignment.topRight,
child: Container(
margin: EdgeInsets.fromLTRB(0.0, 50.0, 0.0, 0.0),
child: Column(
children: <Widget>[
mapButton(_onAddMarkerButtonPressed,
Icon(Icons.add_location), Colors.blue),
// mapButton(
// _onMapTypeButtonPressed,
// Icon(
// IconData(0xf473,
// fontFamily: CupertinoIcons.iconFont,
// fontPackage:
// CupertinoIcons.iconFontPackage),
// ),
// Colors.green),
],
)),
)
]),
),
);
}
}
Solution 1:[1]
The Position could be null. So, you can access the latitude with null operator "!" as
position!.latitude
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | suzan |
