flutter_map Docs
Project Links💝 Support Us
v3
v3
  • flutter_map
  • Getting Started
    • Installation
    • Additional Setup
    • Examples
    • How Does It Work?
      • Raster vs Vector Tiles
  • Usage
    • Base Widget
    • Options
      • Recommended Options
      • Other Options
    • Layers
      • Tile Layer
        • Recommended Options
        • Tile Providers
        • Other Options
      • Marker Layer
      • Polygon Layer
      • Polyline Layer
      • Circle Layer
      • Attribution Layer
      • WMS Usage
    • Controller
    • Full API Reference
  • Plugins
    • Plugins List
    • Making A Plugin
      • Creating New Tile Providers
      • Creating New Layers
  • Tile Servers
    • Using Mapbox
    • Using Stadia Maps
    • Using Thunderforest
    • Offline Mapping
    • Other Options
  • FAQs
    • Frequently Asked Questions
    • Map Controller Issues
  • Migration
    • To v3.0.0
    • To v2.0.0
    • Older Versions
      • To v1.1.0
      • To v1.0.0
      • To v0.15.0
  • Contributing
  • Credits
Powered by GitBook

© flutter_map Authors & Maintainers

On this page
  • Initialisation
  • Usage In initState()
  • Recommended Usage
  • Alternative Usage
  • Available Methods

Was this helpful?

Export as PDF
  1. Usage

Controller

The mapController property takes a MapController, and whilst it is optional, it is strongly recommended for any map other than the most basic. It allows you to programmatically interact with the map, such as panning, zooming and rotating.

Initialisation

To use a MapController, it must initialised and then passed to the FlutterMap. This attaches them until the widget is destroyed/disposed.

final mapController = MapController();

@override
Widget build(BuildContext context) =>
    FlutterMap(
        mapController: mapController,
        ...
    );

Usage In initState()

It is a fairly common requirement to need to use the MapController before the map has been built, or to initialise a listener for one of it's streams inside the initState() StatefulWidget method. Unfortunately, this is not possible, as the map must be built for the controller to be attached.

Recommended Usage

Luckily, Flutter provides methods to wait until the first frame has been built, which usually means the FlutterMap widget will have been built (see exception circumstances below). This makes it trivially easy to implement the desired behaviour.

Recommended Usage
@override
void initState(){
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
        // Use `MapController` as needed
    });
}

Alternative Usage

For simplicity and readability, it is not recommended to use this method unless needed in your situation, although there should be little technical difference.

In some cases, the FlutterMap widget may not have been built on the first frame - for example when using a FutureBuilder around the map.

Alternative Usage
@override
Widget build(BuildContext context) =>
    FlutterMap(
        mapController: mapController,
        options: MapOptions(
            onMapReady: () {
                // Use `MapController` as needed
            },
        ),
    );

Available Methods

PreviousWMS UsageNextPlugins List

Last updated 2 years ago

Was this helpful?

In this case, an alternative method is required to use the MapController on build. This method uses the callback.

MapController class - flutter_map library - Dart API
Full API Reference
Logo
When Map Ready (onMapReady)