Skip to content

Commit

Permalink
fix(#55) : fixes a bug 'makers on the small map not showing' by refac…
Browse files Browse the repository at this point in the history
…toring the code and calling the setState as soon as markers init
  • Loading branch information
i-am-ijaz committed Jun 7, 2023
1 parent dae4a35 commit 37825a0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 37 deletions.
21 changes: 12 additions & 9 deletions lib/app/config/geo_full.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ class _GeoFullState extends State<GeoFull> {
geoService.getCurrentLocation.listen((position) {
centerScreen(position);
});
getAllChildLocations();
_getAllChildLocations();
super.initState();
}

Future<Uint8List> getChildMarkerImage(Map<String, dynamic> data) async {
Future<Uint8List> _getChildMarkerImage(Map<String, dynamic> data) async {
var bytes =
(await NetworkAssetBundle(Uri.parse(data['image'])).load(data['image']))
.buffer
Expand All @@ -54,7 +54,7 @@ class _GeoFullState extends State<GeoFull> {
return bytes;
}

void getAllChildLocations() async {
void _getAllChildLocations() async {
childLocationsList = [];
await FirebaseFirestore.instance
.collection(APIPath.children(_currentUser.uid))
Expand All @@ -63,8 +63,8 @@ class _GeoFullState extends State<GeoFull> {
if (document.docs.isNotEmpty) {
for (var i = 0; i < document.docs.length; i++) {
childLocationsList.add(document.docs[i].data);
initMarker(document.docs[i].data());
getChildMarkerImage(document.docs[i].data());
_initMarker(document.docs[i].data());
_getChildMarkerImage(document.docs[i].data());
debugPrint(
'This is the list of children ${childLocationsList.length}',
);
Expand All @@ -74,8 +74,9 @@ class _GeoFullState extends State<GeoFull> {
}

//TODO:Make function async
Future<List<Marker>> initMarker(Map<String, dynamic> data) async {
Future<List<Marker>> _initMarker(Map<String, dynamic> data) async {
if (data['position'] == null) return [];
allMarkers.clear();

allMarkers.add(
Marker(
Expand All @@ -94,7 +95,10 @@ class _GeoFullState extends State<GeoFull> {
onTap: () {
debugPrint('Marker Tapped');
},
position: LatLng(data['position'].latitude, data['position'].longitude),
position: LatLng(
data['position'].latitude,
data['position'].longitude,
),
),
);

Expand All @@ -116,15 +120,14 @@ class _GeoFullState extends State<GeoFull> {
),
mapType: MapType.normal,
myLocationEnabled: true,
markers: Set<Marker>.of(allMarkers),
onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);
if (allMarkers.isEmpty) return;
setState(() {
markers[MarkerId(allMarkers.first.markerId.value)] =
allMarkers.first;
});
},
markers: Set<Marker>.of(allMarkers),
),
),
);
Expand Down
59 changes: 31 additions & 28 deletions lib/app/config/geo_location.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,28 +60,27 @@ class _GeoState extends State<Geo> {
await FirebaseFirestore.instance
.collection(APIPath.children(_currentUser.uid))
.get()
.then((document) {
if (document.docs.isNotEmpty) {
for (var i = 0; i < document.docs.length; i++) {
childLocationsList.add(document.docs[i].data);
initMarker(document.docs[i].data());
_getChildMarkerImage(document.docs[i].data());
debugPrint(
'This is the list of children ${childLocationsList.length}',
);
.then(
(document) {
if (document.docs.isNotEmpty) {
for (var i = 0; i < document.docs.length; i++) {
childLocationsList.add(document.docs[i].data);
_initMarker(document.docs[i].data());
_getChildMarkerImage(document.docs[i].data());
debugPrint(
'This is the list of children ${childLocationsList.length}',
);
}
}
}
});
},
);
}

//TODO:Make function async
Future<List<Marker>> initMarker(Map<String, dynamic> data) async {
debugPrint('--------------- data -------------');
debugPrint(data['id']);
debugPrint(data['position']?.latitude);
debugPrint(data['position']?.longitude);
Future<List<Marker>> _initMarker(Map<String, dynamic> data) async {
if (data['position'] == null) return [];

allMarkers.clear();

allMarkers.add(
Marker(
infoWindow: InfoWindow(
Expand All @@ -101,10 +100,20 @@ class _GeoState extends State<Geo> {
onTap: () {
debugPrint('Marker Tapped');
},
position: LatLng(data['position'].latitude, data['position'].longitude),
position: LatLng(
data['position'].latitude,
data['position'].longitude,
),
),
);
debugPrint(allMarkers.toString());
if (allMarkers.isEmpty) return [];
if (!mounted) return [];
setState(() {
markers[MarkerId(
allMarkers.first.markerId.value,
)] = allMarkers.first;
});
return allMarkers;
}

Expand All @@ -114,25 +123,19 @@ class _GeoState extends State<Geo> {
height: 300,
margin: EdgeInsets.all(10),
child: GoogleMap(
padding: EdgeInsets.all(10),
mapType: MapType.normal,
myLocationEnabled: true,
markers: Set<Marker>.of(allMarkers),
initialCameraPosition: CameraPosition(
target: LatLng(
widget.initialPosition.latitude,
widget.initialPosition.longitude,
),
zoom: 15,
),
mapType: MapType.normal,
myLocationEnabled: true,
padding: EdgeInsets.all(10),
markers: Set<Marker>.of(allMarkers),
onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);
if (allMarkers.isEmpty) return;
setState(() {
markers[MarkerId(
allMarkers.first.markerId.value,
)] = allMarkers.first;
});
},
),
);
Expand Down

0 comments on commit 37825a0

Please sign in to comment.