I'm trying to access a variable in a class _destLat, and _destLong
final CameraPosition _addBusiness = CameraPosition(
target: LatLng(_destLat, _destLong), zoom: 18.151926040649414);
void initState() {
late double _destLat = widget.restLatitude;
late double _destLong = widget.restLongitude;
super.initState();
}
The error is that _destLat and destLong are undefined.
Here is the full class. This class returns a map widget, on the map widget there are 3 buttons that control the actions on the map, find the marker on map, return to initial position or leave the page.
class BarMapWidget extends StatefulWidget {
final double restLatitude;
final double restLongitude;
const BarMapWidget(
this.restLatitude,
this.restLongitude,
{Key? key}
)
: super(key: key);
@override
_BarMapWidgetState createState() => _BarMapWidgetState();
}
class _BarMapWidgetState extends State<BarMapWidget> {
final Completer<GoogleMapController> _controller = Completer();
Map<MarkerId, Marker> markers = {};
// Destination Longitude
late final double _destLatitude = widget.restLatitude;
late final double _destLongitude = widget.restLongitude;
///////////// Business Position /////////////
final CameraPosition _addBusiness = CameraPosition(
target: LatLng(_destLat, _destLong), zoom: 18.151926040649414);
final CameraPosition _kilkennyCity = const CameraPosition(
target: LatLng(constants.kilkenny_Latitude, constants.kilkenny_Longitude),
zoom: 15.4746,
);
void initState() {
// Add destination marker
_addMarker(
LatLng(_destLatitude, _destLongitude),
"destination",
// BitmapDescriptor.defaultMarkerWithHue(90),
BitmapDescriptor.defaultMarker);
late double _destLat = widget.restLatitude;
late double _destLong = widget.restLongitude;
super.initState();
}
@override
Widget build(BuildContext context) {
return Column(
children: [
Text(_destLatitude.toString()),
Text(_destLongitude.toString()),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
/////////////// Rest Button ///////////////
ElevatedButton(
style: ElevatedButton.styleFrom(
primary: const Color(0xFFd0d0d1),
padding:
const EdgeInsets.symmetric(horizontal: 5, vertical: 10),
),
onPressed: _reset,
child: const Text(
'City Centre ',
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.bold,
color: Color(0xFF0040dd),
),
),
),
/////////////// Find on Map ///////////////
ElevatedButton(
style: ElevatedButton.styleFrom(
primary: const Color(0xFFd0d0d1),
padding:
const EdgeInsets.symmetric(horizontal: 5, vertical: 10),
),
onPressed: _findOnMap,
child: const Text(
'Find on map',
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.bold,
color: Color(0xFF0040dd),
),
),
),
/////////////// All Listings ///////////////
ElevatedButton(
style: ElevatedButton.styleFrom(
primary: const Color(0xFFd0d0d1),
padding:
const EdgeInsets.symmetric(horizontal: 5, vertical: 10),
),
onPressed: () {
Navigator.pop(context);
},
child: const Text(
'All',
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.bold,
color: Color(0xFF0040dd),
),
),
),
],
),
const SizedBox(height: 10),
SizedBox(
height: 500,
child: GoogleMap(
mapType: MapType.normal,
initialCameraPosition: _kilkennyCity,
markers: Set<Marker>.of(markers.values),
myLocationEnabled: false,
myLocationButtonEnabled: false,
tiltGesturesEnabled: true,
compassEnabled: true,
scrollGesturesEnabled: true,
zoomGesturesEnabled: true,
onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);
},
),
),
],
);
}
// This method will add markers to the map based on the LatLng position
_addMarker(LatLng position, String id, BitmapDescriptor descriptor) {
MarkerId markerId = MarkerId(id);
Marker marker =
Marker(markerId: markerId, icon: descriptor, position: position);
markers[markerId] = marker;
}
Future<void> _findOnMap() async {
final GoogleMapController controller = await _controller.future;
controller.animateCamera(CameraUpdate.newCameraPosition(_addBusiness));
}
Future<void> _reset() async {
final GoogleMapController controller = await _controller.future;
controller.animateCamera(CameraUpdate.newCameraPosition(_kilkennyCity));
}
}
CodePudding user response:
Problem:
The variables _destLat
and _destLong
are only defined within the scope of the initState
method and that makes them undefined within the class.
Solution:
You can make them available to _addBusiness
by defining them outside the initState
method in the state class.
And since you need the _destLat
and _destLong
variables to define the _addBusiness
variable, you should put the assignment for _addBusiness
right after the assignments for _destLat
and _destLong
.
Here is a code demonstration:
late double _destLat;
late double _destLong;
late CameraPosition _addBusiness;
void initState() {
// Add destination marker
_addMarker(
LatLng(_destLatitude, _destLongitude),
"destination",
// BitmapDescriptor.defaultMarkerWithHue(90),
BitmapDescriptor.defaultMarker);
_destLat = widget.restLatitude;
_destLong = widget.restLongitude;
_addBusiness = CameraPosition(
target: LatLng(_destLat, _destLong), zoom: 18.151926040649414);
super.initState();
}