i am learning flutter and trying to create google map with markers obtained from firebase. The problem is that the following code can only show the map without markers. I can see from the output that the geopoint data can be obtained from firebase successfully.
// home.dart
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:geolocator/geolocator.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
// import 'package:firebase_analytics/firebase_analytics.dart';
// import 'package:firebase_auth/firebase_auth.dart';
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
bool mapToggle = false;
GoogleMapController mapController;
Position _currentPosition;
List<Marker> allMarkers = [];
var clients = [];
_getCurrentLocation() async {
Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.best)
.then((Position position) {
setState(() {
_currentPosition = position;
mapToggle = true;
_populateClients();
});
}).catchError((e) {
print(e);
});
}
_populateClients() {
CollectionReference ref = FirebaseFirestore.instance.collection('markers');
ref.get().then((doc) {
if (doc.docs.isNotEmpty) {
for (int i = 0; i < doc.docs.length; i++) {
print('$i');
print(doc.docs[i].data());
print(doc.docs[i].data()['clientName']);
print(doc.docs[i].data()['location'].latitude);
print(doc.docs[i].data()['location'].longitude);
allMarkers.add(Marker(
markerId: MarkerId(doc.docs[i].data()['clientName']),
position: LatLng(doc.docs[i].data()['location'].latitude, doc.docs[i].data()['location'].longitude),
draggable: false,
));
}
}
});
}
@override
void initState() {
// TODO: implement initState
super.initState();
_getCurrentLocation();
}
void _onMapCreated(controller) {
setState(() {
mapController = controller;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Home'),
),
body: mapToggle
? GoogleMap(
onMapCreated: _onMapCreated,
initialCameraPosition: CameraPosition(
target: LatLng(
_currentPosition.latitude, _currentPosition.longitude),
zoom: 10.0),
markers: Set.from(allMarkers),
)
: Center(
child: Text(
'Map Loading.. Please wait..',
style: TextStyle(fontSize: 20.0),
)));
}
}
I follow this tutorial but many of the code is deprecated: https://youtu.be/Hg8CT3ysFjY
below is the output of the Run tab:
Launching lib/main.dart on ONEPLUS A6003 in debug mode...
Running Gradle task 'assembleDebug'...
✓ Built build/app/outputs/flutter-apk/app-debug.apk.
Waiting for ONEPLUS A6003 to report its views...
Debug service listening on ws://127.0.0.1:43105/bLGH86hwpxI=/ws
Syncing files to device ONEPLUS A6003...
W/DynamiteModule(30614): Local module descriptor class for providerinstaller not found.
I/DynamiteModule(30614): Considering local module providerinstaller:0 and remote module providerinstaller:0
W/ProviderInstaller(30614): Failed to load providerinstaller module: No acceptable module found. Local version is 0 and remote version is 0.
I/ter_geolocatio(30614): The ClassLoaderContext is a special shared library.
I/ter_geolocatio(30614): The ClassLoaderContext is a special shared library.
V/NativeCrypto(30614): Registering com/google/android/gms/org/conscrypt/NativeCrypto's 288 native methods...
.
.
.
W/ter_geolocatio(30614): Found duplicate classes, falling back to extracting from APK : /data/user_de/0/com.google.android.gms/app_chimera/m/000001dd/MapsDynamite.apk
W/ter_geolocatio(30614): NOTE: This wastes RAM and hurts startup performance.
W/ter_geolocatio(30614): Found duplicated class when checking oat files: 'Landroidx/annotation/Keep;' in /data/app/com.matthiaschan.flutter_geolocation-YwDUb2lByOR1j5fFIfFZbA==/base.apk and /data/user_de/0/com.google.android.gms/app_chimera/m/000001dd/MapsDynamite.apk
W/ter_geolocatio(30614):
W/Gralloc3(30614): allocator 3.x is not supported
W/ter_geolocatio(30614): Accessing hidden method Lsun/misc/Unsafe;->getObject(Ljava/lang/Object;J)Ljava/lang/Object; (greylist, linking, allowed)
W/ter_geolocatio(30614): Accessing hidden method Lsun/misc/Unsafe;->putObject(Ljava/lang/Object;JLjava/lang/Object;)V (greylist, linking, allowed)
I/Google Maps Android API(30614): Google Play services client version: 12451000
I/Google Maps Android API(30614): Google Play services package version: 204217037
.
.
.I/flutter (30614): 0
W/ter_geolocatio(30614): Accessing hidden method Lsun/misc/Unsafe;->putInt(Ljava/lang/Object;JI)V (greylist, linking, allowed)
W/ter_geolocatio(30614): Accessing hidden method Lsun/misc/Unsafe;->getObject(Ljava/lang/Object;J)Ljava/lang/Object; (greylist, linking, allowed)
W/ter_geolocatio(30614): Accessing hidden method Lsun/misc/Unsafe;->putObject(Ljava/lang/Object;JLjava/lang/Object;)V (greylist, linking, allowed)
I/flutter (30614): {clientName: 5507, location: Instance of 'GeoPoint'}
I/flutter (30614): 5507
I/flutter (30614): 22.30797605
I/flutter (30614): 114.2287517
I/flutter (30614): 1
I/flutter (30614): {clientName: 5515, location: Instance of 'GeoPoint'}
I/flutter (30614): 5515
I/flutter (30614): 22.2783769
I/flutter (30614): 114.1789305
I/flutter (30614): 2
I/flutter (30614): {clientName: 5517, location: Instance of 'GeoPoint'}
I/flutter (30614): 5517
I/flutter (30614): 22.29564323
I/flutter (30614): 114.1717511
I/flutter (30614): 3
I/flutter (30614): {clientName: 5516, location: Instance of 'GeoPoint'}
I/flutter (30614): 5516
I/flutter (30614): 22.27870029
I/flutter (30614): 114.1839518
W/ter_geolocatio(30614): Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
W/ter_geolocatio(30614): Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
W/ter_geolocatio(30614): Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
W/ter_geolocatio(30614): Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
W/ter_geolocatio(30614): Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
W/ter_geolocatio(30614): Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
W/ter_geolocatio(30614): Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
W/ter_geolocatio(30614): Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
W/ter_geolocatio(30614): Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
W/ter_geolocatio(30614): Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
W/ter_geolocatio(30614): Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
W/ter_geolocatio(30614): Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
W/ConnectionTracker(30614): Exception thrown while unbinding
W/ConnectionTracker(30614): java.lang.IllegalArgumentException: Service not registered: lp@df9eea1
W/ConnectionTracker(30614): at android.app.LoadedApk.forgetServiceDispatcher(LoadedApk.java:1766)
W/ConnectionTracker(30614): at android.app.ContextImpl.unbindService(ContextImpl.java:1810)
W/ConnectionTracker(30614): at android.content.ContextWrapper.unbindService(ContextWrapper.java:741)
W/ConnectionTracker(30614): at ci.f(:com.google.android.gms.dynamite_measurementdynamite@[email protected] (120400-0):1)
W/ConnectionTracker(30614): at ci.d(:com.google.android.gms.dynamite_measurementdynamite@[email protected] (120400-0):2)
W/ConnectionTracker(30614): at lq.D(:com.google.android.gms.dynamite_measurementdynamite@[email protected] (120400-0):10)
W/ConnectionTracker(30614): at lc.a(:com.google.android.gms.dynamite_measurementdynamite@[email protected] (120400-0):2)
W/ConnectionTracker(30614): at ee.run(:com.google.android.gms.dynamite_measurementdynamite@[email protected] (120400-0):3)
W/ConnectionTracker(30614): at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:462)
W/ConnectionTracker(30614): at java.util.concurrent.FutureTask.run(FutureTask.java:266)
W/ConnectionTracker(30614): at ix.run(:com.google.android.gms.dynamite_measurementdynamite@[email protected] (120400-0):6)
W/DynamiteModule(30614): Local module descriptor class for com.google.android.gms.googlecertificates not found.
I/DynamiteModule(30614): Considering local module com.google.android.gms.googlecertificates:0 and remote module com.google.android.gms.googlecertificates:6
I/DynamiteModule(30614): Selected remote version of com.google.android.gms.googlecertificates, version >= 6
I/DynamiteLoaderV2Impl(30614): [71] Googlecertificates
W/ter_geolocatio(30614): ClassLoaderContext type mismatch. expected=PCL, found=DLC (PCL[] | DLC[];PCL[/data/app/com.matthiaschan.flutter_geolocation-YwDUb2lByOR1j5fFIfFZbA==/base.apk*1782575538:/data/app/com.matthiaschan.flutter_geolocation-YwDUb2lByOR1j5fFIfFZbA==/base.apk!classes2.dex*382790293:/data/app/com.matthiaschan.flutter_geolocation-YwDUb2lByOR1j5fFIfFZbA==/base.apk!classes3.dex*184402630:/data/app/com.matthiaschan.flutter_geolocation-YwDUb2lByOR1j5fFIfFZbA==/base.apk!classes4.dex*3697552760:/data/app/com.matthiaschan.flutter_geolocation-YwDUb2lByOR1j5fFIfFZbA==/base.apk!classes5.dex*3148882451:/data/app/com.matthiaschan.flutter_geolocation-YwDUb2lByOR1j5fFIfFZbA==/base.apk!classes6.dex*4130421832:/data/app/com.matthiaschan.flutter_geolocation-YwDUb2lByOR1j5fFIfFZbA==/base.apk!classes7.dex*2561763281:/data/app/com.matthiaschan.flutter_geolocation-YwDUb2lByOR1j5fFIfFZbA==/base.apk!classes8.dex*3579848402]{PCL[/system/framework/org.apache.http.legacy.jar*4222887692]})
W/ter_geolocatio(30614): Found duplicate classes, falling back to extracting from APK : /data/user_de/0/com.google.android.gms/app_chimera/m/000001dc/GoogleCertificates.apk
W/ter_geolocatio(30614): NOTE: This wastes RAM and hurts startup performance.
W/ter_geolocatio(30614): Found duplicated class when checking oat files: 'Landroidx/annotation/Keep;' in /data/app/com.matthiaschan.flutter_geolocation-YwDUb2lByOR1j5fFIfFZbA==/base.apk and /data/user_de/0/com.google.android.gms/app_chimera/m/000001dc/GoogleCertificates.apk
W/ter_geolocatio(30614):
W/ter_geolocatio(30614): Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
I/chatty (30614): uid=10445(com.matthiaschan.flutter_geolocation) androidmapsapi- identical 27 lines
W/ter_geolocatio(30614): Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
W/ter_geolocatio(30614): Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
I/chatty (30614): uid=10445(com.matthiaschan.flutter_geolocation) GoogleApiHandle identical 27 lines
W/ter_geolocatio(30614): Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
W/ter_geolocatio(30614): Accessing hidden method Lsun/misc/Unsafe;->getObject(Ljava/lang/Object;J)Ljava/lang/Object; (greylist, linking, allowed)
W/ter_geolocatio(30614): Accessing hidden method Lsun/misc/Unsafe;->getObject(Ljava/lang/Object;J)Ljava/lang/Object; (greylist, linking, allowed)
W/ter_geolocatio(30614): Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
W/ConnectionTracker(30614): Exception thrown while unbinding
W/ConnectionTracker(30614): java.lang.IllegalArgumentException: Service not registered: lp@df9eea1
W/ConnectionTracker(30614): at android.app.LoadedApk.forgetServiceDispatcher(LoadedApk.java:1766)
W/ConnectionTracker(30614): at android.app.ContextImpl.unbindService(ContextImpl.java:1810)
W/ConnectionTracker(30614): at android.content.ContextWrapper.unbindService(ContextWrapper.java:741)
W/ConnectionTracker(30614): at ci.f(:com.google.android.gms.dynamite_measurementdynamite@[email protected] (120400-0):1)
W/ConnectionTracker(30614): at ci.d(:com.google.android.gms.dynamite_measurementdynamite@[email protected] (120400-0):2)
W/ConnectionTracker(30614): at lq.D(:com.google.android.gms.dynamite_measurementdynamite@[email protected] (120400-0):10)
W/ConnectionTracker(30614): at lc.a(:com.google.android.gms.dynamite_measurementdynamite@[email protected] (120400-0):2)
W/ConnectionTracker(30614): at ee.run(:com.google.android.gms.dynamite_measurementdynamite@[email protected] (120400-0):3)
W/ConnectionTracker(30614): at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:462)
W/ConnectionTracker(30614): at java.util.concurrent.FutureTask.run(FutureTask.java:266)
W/ConnectionTracker(30614): at ix.run(:com.google.android.gms.dynamite_measurementdynamite@[email protected] (120400-0):6)
V/NativeCrypto(30614): SSL shutdown failed: ssl=0x7ded8b8048: I/O error during system call, Broken pipe
W/ter_geolocatio(30614): Accessing hidden method Ldalvik/system/CloseGuard;->close()V (greylist,core-platform-api, linking, allowed)
First of you can't call an async function (getCurrentLocation)in the initState, because it is synchronous initially. Better call it in the onMapCreated function.
Second try using this code for adding the markers to the list of markers: Inside the "for" function in populateClient:
Third use a setState around your "for". Finally you need to call the function in onMapCreated.
I usually don't work with firebase, but still I think this will help with your problem.