I have a flutter projects which aims to show current location of my cars in Mapbox. I use 'mapbox_gl (0.1.0)' package and show location of my cars with addSymbol function of MapboxMapController in MapboxMap widget. My MapboxMap widget is in StreamBuilder and I read locations of my cars from Firebase Realtime Database. The problem is when I change the location of my car from database the current location doesn't show up. Also, when I click hot reload button the location doesn't change as well.
Here is my code:
StreamBuilder(
stream: FirebaseDatabase.instance
.reference()
.child("Vehicle")
.onValue,
builder: (context, snapShot) {
if (snapShot.hasData) {
print(snapShot.data.snapshot.value.toString());
Map data = snapShot.data.snapshot.value;
List item = [];
data.forEach(
(index, data) => item.add({"key":index, ...data}));
return MapboxMap(
accessToken: token,
initialCameraPosition: CameraPosition(
zoom: 15,
target: LatLng(38.470353, 27.076155),
),
onMapCreated:
(MapboxMapController controller) async {
final result = await acquireCurrentLocation();
//this line moves camera to my current location
await controller.moveCamera(
CameraUpdate.newLatLng(result),
);
await controller.addCircle(
CircleOptions(
circleRadius: 8.0,
circleColor: '#006992',
circleOpacity: 0.8,
geometry: result,
draggable: false,
),
);
await controller.addSymbol(SymbolOptions(
geometry: LatLng(item[0]['location']['latitude'], item[0]['location']['longitude']),
iconSize: 0.5,
iconImage: getIconImage(double.parse(item[0]['battery_percent'])),
draggable: false,
));
await controller.addSymbol(
SymbolOptions(
geometry: LatLng(item[1]['location']['latitude'], item[1]['location']['longitude']),
draggable: false,
iconSize: 0.5,
iconImage: getIconImage(double.parse(item[1]['battery_percent'])),
),
);
});
}
if (snapShot.hasError) {
return Text("Error");
} else {
return Center(child: CircularProgressIndicator());
}
});
Why do hot reload and changes in database not affect the map. The symbols that show the locations of my cars stay in the same place unless I rerun the application? What should I do?