// ignore_for_file: use_key_in_widget_constructors, prefer_final_fields, prefer_const_constructors, duplicate_ignore, camel_case_types, unnecessary_new, unnecessary_this, unnecessary_null_comparison
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:location/location.dart';
class map extends StatefulWidget {
@override
State<map> createState() => mapState();
}
class mapState extends State<map> {
late GoogleMapController _controller;
LatLng? latlng;
late StreamSubscription _locationSubscription;
Location _locationTracker = Location();
Marker marker ;
Circle circle;
// ignore: prefer_const_constructors
static CameraPosition _kGooglePlex = CameraPosition(
target: LatLng(37.42796133580664, -122.085749655962),
zoom: 15,
);
Future<Uint8List> getMarker() async {
ByteData byteData = await DefaultAssetBundle.of(context).load("assets/car_icon.png");
return byteData.buffer.asUint8List();
}
static final CameraPosition _kLake = CameraPosition(
bearing: 192.8334901395799,
target: LatLng(37.43296265331129, -122.08832357078792),
tilt: 59.440717697143555,
zoom: 19.151926040649414);
void updateMarkerAndCircle(LocationData newLocalData, Uint8List imageData) {
LatLng latlng = LatLng(newLocalData.latitude!, newLocalData.longitude!);
this.setState(() {
marker = Marker(
markerId: MarkerId("home"),
position: latlng,
rotation: newLocalData.heading!,
draggable: false,
zIndex: 2,
flat: true,
anchor: Offset(0.5, 0.5),
icon: BitmapDescriptor.fromBytes(imageData));
circle = Circle(
circleId: CircleId("car"),
radius: newLocalData.accuracy!,
zIndex: 1,
strokeColor: Colors.blue,
center: latlng,
fillColor: Colors.blue.withAlpha(70));
});
}
void getCurrentLocation() async {
try {
Uint8List imageData = await getMarker();
var location = await _locationTracker.getLocation();
updateMarkerAndCircle(location, imageData);
if (_locationSubscription != null) {
_locationSubscription.cancel();
}
_locationSubscription = _locationTracker.onLocationChanged.listen((newLocalData) {
if (_controller != null) {
_controller.animateCamera(CameraUpdate.newCameraPosition(new CameraPosition(
bearing: 192.8334901395799,
target: LatLng(newLocalData.latitude!, newLocalData.longitude!),
tilt: 0,
zoom: 18.00)));
updateMarkerAndCircle(newLocalData, imageData);
}
});
} on PlatformException catch (e) {
if (e.code == 'PERMISSION_DENIED') {
debugPrint("Permission Denied");
}
}
}
@override
void dispose() {
if (_locationSubscription != null) {
_locationSubscription.cancel();
}
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: GoogleMap(
mapType: MapType.hybrid,
myLocationButtonEnabled: true,
myLocationEnabled: true,
initialCameraPosition: _kGooglePlex,
markers: Set.of((marker != null) ? [marker] : []),
circles: Set.of((circle != null) ? [circle] : []),
onMapCreated: (GoogleMapController controller) {
_controller = controller;
},
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
floatingActionButton: FloatingActionButton.extended(
onPressed: getCurrentLocation,
label: Text('To the lake!'),
icon: Icon(Icons.location_searching),
),
);
}
}
This is original code in which i’m facing the error. when i define Marker marker
like this it says maker should be initialise
, when i define it like late Marker marker
then code dosen’t shows any error but in app it says LateInitialization error : Field marker has not been initialized
, and third one when i define it like Marker? marker
, while passinf the value marker in GoogleMap function like this markers: Set.of((marker != null) ? [marker] : [])
shows the error The element type 'Circle?' can't be assigned to the list type 'Circle'.
im using vscode and dependencies are
google_maps_flutter: ^2.2.0
location: ^4.4.0
2
Answers
I suggest that you change the declaration from either
to
As you already perform a null check on the build method, this should work.
EDIT:
Since your
Marker
instance should be a nullable now, then you can rephrase the line where you assign the markers onGoogleMap
constructor like thisChange marker to this:
then in your updateMarkerAndCircle, do this:
then in your GoogleMap widget, do this: