'I can't display the markers of my Firebase database
I'm really lost I don't know how to solve the problem by the way if you find another error or a way of doing things that isn't the right one I'm interested in advice
Here is the firebase reading code I made
//############################
//# Markers Builder FireBase #
//############################
Map<MarkerId, Marker> markers = <MarkerId, Marker>{};
getMarkerData() async {
FirebaseFirestore.instance.collection('Markers').get().then((myMarkers) {
if (myMarkers.docs.isNotEmpty) {
for (int i = 0; i < myMarkers.docs.length; i++) {
initMarker(myMarkers.docs[i].data, myMarkers.docs[i].id);
}
}
});
}
void initMarker(specify, specifyId) {
var markerIdVal = specifyId;
final MarkerId markerId = MarkerId(markerIdVal);
final Marker marker = Marker(
markerId: markerId,
position:
LatLng(specify['Location'].latitude, specify['Location'].longitude),
infoWindow: InfoWindow(snippet: specify['Test data Firebase Marker']),
);
setState(() {
markers[markerId] = marker;
});
}
here is the error message
Restarted application in 654ms.
[VERBOSE-2:ui_dart_state.cc(209)] Unhandled Exception: NoSuchMethodError: Closure call with mismatched arguments: function '[]'
Receiver: Closure: () => Map<String, dynamic> from Function 'data':.
Tried calling: []("Location")
Found: []() => Map<String, dynamic>
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:38:5)
#1 PageMapsState.initMarker
#2 PageMapsState.getMarkerData.<anonymous closure>
package:noname/views/mappage.dart:144
#3 _rootRunUnary (dart:async/zone.dart:1434:47)
#4 _CustomZone.runUnary (dart:async/zone.dart:1335:19)
<asynchronous suspension>
Application finished.
here is the complete code
import 'dart:async';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:noname/blocs/geolocation/geolocation_bloc.dart';
import 'package:noname/onboarding/onboarding_font.dart';
import 'package:noname/widget/others/utils.dart';
import 'package:sliding_up_panel/sliding_up_panel.dart';
class PageMaps extends StatefulWidget {
@override
PageMapsState createState() => PageMapsState();
}
//###########################
//# Initialisation générale #
//###########################
class UserLocation {
final double latitude;
final double longitude;
UserLocation(this.latitude, this.longitude);
}
class PageMapsState extends State<PageMaps> {
Completer<GoogleMapController> _controller = Completer();
@override
void initState() {
getMarkerData();
super.initState();
}
//##########################
//# Launch Widget générale #
//##########################
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: <Widget>[
_BottomsSliderInfo(),
_Searchbar(),
],
),
);
}
//#####################
//# Widget Search bar #
//#####################
Widget _Searchbar() {
return SafeArea(
child: SizedBox(
width: 900.0,
height: 60.0,
child: Card(
child: Center(
child: Text(
'Search bar a DEV!! + Bouton filtre',
style: temp,
),
),
color: Colors.blue,
),
));
}
//###########################
//# Widget SliderInfoBottom #
//###########################
Widget _BottomsSliderInfo() {
return Scaffold(
body: SlidingUpPanel(
panel: Center(
child: Text(
"Information a développer",
style: temp,
),
),
body: Stack(
children: <Widget>[
_buildGoogleMap(context),
],
)),
);
}
//###################################
//# Widget Constructeur Google Maps #
//###################################
Widget _buildGoogleMap(BuildContext context) {
return Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
child: BlocBuilder<GeolocationBloc, GeolocationState>(
builder: (context, state) {
if (state is GeolocationLoading) {
return Center(
child: CircularProgressIndicator(),
);
} else if (state is GeolocationLoaded) {
return GoogleMap(
initialCameraPosition: CameraPosition(
target:
LatLng(state.position.latitude, state.position.longitude),
zoom: 14.5),
onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);
controller.setMapStyle(Utils.MapStyle);
},
myLocationButtonEnabled: false,
minMaxZoomPreference: MinMaxZoomPreference(8, 25),
myLocationEnabled: true,
markers: Set<Marker>.of(markers.values),
// markers: {
// Bruxelles_central,
// paparoti,
// test,
// },
);
} else {
return Text('Erreur de localisation');
}
},
),
);
}
//############################
//# Markers Builder FireBase #
//############################
Map<MarkerId, Marker> markers = <MarkerId, Marker>{};
getMarkerData() async {
FirebaseFirestore.instance.collection('Markers').get().then((myMarkers) {
if (myMarkers.docs.isNotEmpty) {
for (int i = 0; i < myMarkers.docs.length; i++) {
initMarker(myMarkers.docs[i].data, myMarkers.docs[i].id);
}
}
});
}
void initMarker(specify, specifyId) {
var markerIdVal = specifyId;
final MarkerId markerId = MarkerId(markerIdVal);
final Marker marker = Marker(
markerId: markerId,
position:
LatLng(specify['Location'].latitude, specify['Location'].longitude),
infoWindow: InfoWindow(snippet: specify['Test data Firebase Marker']),
);
setState(() {
markers[markerId] = marker;
});
}
// Set<Marker> getMarker() {
// return <Marker>[
// Marker(
// markerId: MarkerId('Test'),
// position: LatLng(37.783739514044825, -122.4084728076636),
// icon: BitmapDescriptor.defaultMarker,
// )
// ].toSet();
// }
}
I found a lot of post about this topic but nothing works Thanks for reading the post
Solution 1:[1]
Try this:
getMarkerData() async {
FirebaseFirestore.instance.collection('Markers').get().then((DocumentSnapshot myMarkers) {
if (myMarkers.exists) {
var data = myMarkers.data();
var res = data as Map<String, dynamic>;
res.forEach((e){
//initMarker();
});
}
});
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Saitoh Akira |
