Home > Mobile >  Flutter; Passing Set<Marker> _markers = {} from one screen to another
Flutter; Passing Set<Marker> _markers = {} from one screen to another

Time:11-26

I'm making a flutter app that adds markers to a map based on the current location. I have an info screen that takes user info and passes that info to a marker to be added on the flutter google maps. I'm having issues passing the Set of markers to my map_view.dart. The following is the Future function for adding markers to the map from the current location and then navigating to the map screen with the _marker set declared in the

making a Future function called _addMarker() which would get the user's current location, place a marker on that point, and navigate back to the map_view.dart screen

info_window.dart screen.

    final Set<Marker> _markers = {};
    Future<void> _addMarker() async {
    getUserCurrentLocation().then((value) async {
      //print(value.latitude.toString()   " "   value.longitude.toString());
      String ID;
      // marker added for current users location
      int x = 2;
      x   1;
      ID = x.toString();

      _markers.add(Marker(
        markerId: MarkerId(ID), //this can be taken from the other screen maybe
        position: LatLng(value.latitude, value.longitude),
        infoWindow: InfoWindow(
          title: 'My Current Location',
          snippet: 'captured from info window too',
          onTap: () => _goToInfoScreen(),
        ),
      ));
      Navigator.push(
          context,
          MaterialPageRoute(
            builder: (context) => MapSample(_markers),
          ));
    });


        setState(() {
          //_markers = result as Set<Marker>;
        });
        //});

      }

And have this for my map_view.dart screen trying to get the marker set to be used in this screen

map_view.dart screen


class MapSample extends StatefulWidget {
  //Set<Marker> markers = {};

  const MapSample(Set<Marker> marker, {Key? key}) : super(key: key);

  @override
  State<MapSample> createState() => MapSampleState();
}

//late final Set<Marker> _markers;

//marker = MapSample(_markers);

class MapSampleState extends State<MapSample> {
...

I'm struggling with using this variable after it gets passed to this screen, when trying to use the marker variable to add the markers to the map they never appear. Is there another/any way I should be retrieving the set in the map_view.dart screen after it's passed in from the navigator on the info_window.dart screen?

CodePudding user response:

If you want to use variables inside State class that is coming from main class that extends StatefulWidget, you need to use widget.variable. In your case it is widget.marker.

class MapSample extends StatefulWidget {
  //Set<Marker> markers = {};

  const MapSample(Set<Marker> marker, {Key? key}) : super(key: key);

  @override
  State<MapSample> createState() => MapSampleState();
}

//late final Set<Marker> _markers;

//marker = MapSample(_markers);

class MapSampleState extends State<MapSample> {

  late Type foo;

  @override
  void initState() {
    foo = widget.foo;
    super.initState();
}
  • Related