controller property Null safety

MapShapeLayerController? controller
final

Provides option for adding, removing, deleting and updating marker collection.

You can also get the current markers count and selected shape's index from this.

late List<Model> _data;
late MapShapeLayerController _controller;
late MapShapeSource _mapSource;

Random _random = Random();

@override
void initState() {
  _data = <Model>[
     Model(-14.235004, -51.92528),
     Model(51.16569, 10.451526),
     Model(-25.274398, 133.775136),
     Model(20.593684, 78.96288),
     Model(61.52401, 105.318756)
  ];

  _mapSource = MapShapeSource.asset(
     "assets/world_map.json",
     shapeDataField: "name",
  );

  _controller = MapShapeLayerController();
  super.initState();
}

@override
Widget build(BuildContext context) {
  return Scaffold(
    body: Center(
      child: Container(
        height: 350,
        child: Padding(
          padding: EdgeInsets.only(left: 15, right: 15),
          child: Column(
            children: [
              SfMaps(
                layers: <MapLayer>[
                  MapShapeLayer(
                   source: _mapSource,
                    initialMarkersCount: 5,
                    markerBuilder: (BuildContext context, int index) {
                      return MapMarker(
                        latitude: _data[index].latitude,
                        longitude: _data[index].longitude,
                        child: Icon(Icons.add_location),
                      );
                    },
                    controller: _controller,
                  ),
                ],
              ),
              RaisedButton(
                child: Text('Add marker'),
                onPressed: () {
                  _data.add(Model(
                      -180 + _random.nextInt(360).toDouble(),
                      -55 + _random.nextInt(139).toDouble()));
                  _controller.insertMarker(5);
                },
              ),
            ],
          ),
        ),
      ),
    ),
  );
}

class Model {
 Model(this.latitude, this.longitude);

 final double latitude;
 final double longitude;
}

Implementation

final MapShapeLayerController? controller;