Luthermilla Ecole
Luthermilla Ecole

Reputation: 786

Marker won't be displayed in Flutter

I am a little new to the flutter world and I am trying to display a set of markers in my Map from some values in my firebase database. But so far the markers won't appear, sometimes after a hot reload a marker will appear, but its nowhere near what I have in the database. Can someone please help me, I have tried almost everything. Here is the code:

        class Routes extends StatefulWidget{
        static const routeName = '/routes';
        @override
       State<StatefulWidget> createState() => new _RoutesState();
        }

         class _RoutesState extends State <Routes> { 
         GoogleMapController _controller;

          Set <Marker> _markers ={};

         void _onMapCreated(GoogleMapController controller){
          setState(() {
             _controller = controller;
             retrieveData();
           });
          }

             void iniState(){
            super.initState();
            setState(() {
            retrieveData();
              });
            }

         retrieveData(){
         setState(() async {
         await Firebase.initializeApp();
         final firestoreInstance = FirebaseFirestore.instance;
         String place;
         double rlat,rlong,speed;
         String emailUser = FirebaseAuth.instance.currentUser.email;

    firestoreInstance.collection('Cars').where('email', isEqualTo : emailUser).get().then((value){
      value.docs.forEach((result) {
         print(result.data());

         place = result.data()['place'] ;
         rlat = double.parse(result.data()['rlat']);
         rlong =  double.parse(result.data()['rlong']);

         _markers.add(
           Marker(
             markerId: MarkerId('Details'),
             position: LatLng(rlat,rlong),
             )
         );
     });
 });
 });
           }

   Widget build(BuildContext context) {
   return Scaffold(
    appBar: new AppBar(
   title: new Text('Track Route',
   style: new TextStyle(color:Colors.white, fontWeight:FontWeight.bold),
   ),
   leading: IconButton(
    icon: Icon(Icons.arrow_back, 
   color: Colors.white),
     onPressed: (){
     Navigator.of(context).popAndPushNamed(RootPage.routeName);
  },
  ),
 ),
 body: Stack(
   children: <Widget>[
     GoogleMap(
       onMapCreated:_onMapCreated,
       initialCameraPosition: CameraPosition(target: LatLng(0,0),zoom:4) ,
       markers: _markers,
       )
   ],
 ) ,
   );
 }
}

Upvotes: 0

Views: 520

Answers (1)

Payam Asefi
Payam Asefi

Reputation: 2757

What happens when you change this line:

         _markers.add(
       Marker(
         markerId: MarkerId('Details'),
         position: LatLng(rlat,rlong),
         )
     );

To this:

        setState(() {
            _markers.add(
              Marker(
               markerId: MarkerId('Details'),
               position: LatLng(rlat,rlong),
              )
           );
          });

Upvotes: 1

Related Questions