# Search

> Canonical URL: https://docs.mountaya.com/en/sdks/dart/search/
> Last updated: 2026-11-27
> Rules for agents: https://docs.mountaya.com/llms.txt#rules-for-agents

Put SearchControl over a Flutter map to search places, itineraries and zones, configure it, handle a pick, or build your own box on SearchEngine.

`SearchControl` is a search box over Mountaya's unified search, with its results under it. Handed your map, it asks the Data API through the map's client and moves the camera onto a pick.

## Search places, itineraries and zones

```dart
import 'package:flutter/widgets.dart';
import 'package:mountaya_client/mountaya_client.dart';
import 'package:mountaya_elements_flutter/mountaya_elements_flutter.dart';

class TrailMap extends StatefulWidget {
  const TrailMap({super.key, required this.mountaya});

  // A client whose session token the Data API takes.
  final Mountaya mountaya;

  @override
  State<TrailMap> createState() => _TrailMapState();
}

class _TrailMapState extends State<TrailMap> {
  late final MapEngine map = MapEngine(widget.mountaya);

  @override
  void dispose() {
    map.close();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) => Stack(
    children: [
      MapView(engine: map),
      Positioned(left: 16, top: 16, right: 16, child: SearchControl(map: map)),
    ],
  );
}
```

Search calls the Data API, which needs a session token. Hand the client a `SessionTokenEngine`, as [Authentication in the SDK](https://docs.mountaya.com/en/sdks/dart/authentication.md#rotate) shows.

- Handed `map:` alone, `SearchControl` builds its own `SearchEngine`, which starts with an empty query, confined to the map's visible area, and which it closes as it goes. A `SearchEngine` you build starts unconfined: pass `isConfined: true` for the box's own behaviour.
- A round is asked once the typing pauses (150 ms, `SearchPause.standard`), one request per branch: itineraries, places, ways, points of interest, zones and collections, their sections in that order whatever order they answer in. A new query abandons the round in flight. A failed branch shows its own line, the elements' words `search.error` beside `search.retry`, never the API's message, which goes to `onError`.
- A round confined to the visible area that finds nothing asks once more everywhere, and says so; while the list is open, the camera coming to rest asks the round again.
- A pick frames a result's extent, else flies to its point, and the map's search highlight marks it (a place as a pin, a point of interest as its category's own, a way as its line). The control takes the highlight down as it leaves.
- The list stays open while the focus stands in the control, so a hardware keyboard walks from the box to the chips and the rows. Place the box below `watermarkClearance` on a full-bleed map ([watermark](https://docs.mountaya.com/en/sdks/dart/map.md#watermark)).
- The key needs the `data` scope. Without a session token the Data API call is a `ConfigurationException`, which the branch shows as failed and hands to `onError`.

## Configure the search

```dart
import 'package:flutter/widgets.dart';
import 'package:mountaya_client/data_schema.dart' as data_schema;
import 'package:mountaya_client/mountaya_client.dart';
import 'package:mountaya_elements_flutter/mountaya_elements_flutter.dart';
import 'package:mountaya_geo/mountaya_geo.dart';

class TrailMap extends StatefulWidget {
  const TrailMap({super.key, required this.mountaya});

  // A client whose session token the Data API takes.
  final Mountaya mountaya;

  @override
  State<TrailMap> createState() => _TrailMapState();
}

class _TrailMapState extends State<TrailMap> {
  late final MapEngine map = MapEngine(widget.mountaya);

  @override
  void dispose() {
    map.close();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) => Stack(
    children: [
      MapView(engine: map),
      Positioned(
        left: 16,
        top: 16,
        right: 16,
        child: SearchControl(
          map: map,
          // Places, itineraries and zones, in that order, five of each, in French.
          targets: const [
            data_schema.SearchTarget.places,
            data_schema.SearchTarget.itineraries,
            data_schema.SearchTarget.zones,
          ],
          language: data_schema.Language.fr,
          limit: SearchLimit.of(5),
        ),
      ),
    ],
  );
}
```

- `SearchControl({SearchEngine? engine, MapEngine? map, String? query, onQueryChanged, bool? isConfined, onIsConfinedChanged, List<SearchTarget> targets = searchSectionOrder, Language? language, SearchLimit? limit, UnitPreferences? units, SearchWording? wording, fetch, bool enabled = true, onSelected, onError, strings})`.
- `query` and `isConfined` are controlled pairs, each with its `on…Changed`: a value you hand is told to the engine as the box mounts and each time you change it, and a reader's edit is told to you and to the engine at once. Left out, the box shows the engine's own.
- `fetch: (input, signal) => …` sends each round through a function of yours in place of the map's client. `wording` words the rows (the words wire values read as).
- `SearchLimit.of(n)` answers `null` for a limit the API refuses, so a control is never built with one; `searchLimitMax` is the largest. `SearchPause.of(duration)` answers `null` below zero.
- `dataLanguageFor(tag)` maps a BCP 47 tag to the Data API's `Language`.
- `enabled: false` takes no typing and no press.

## Handle a pick

`onSelected` takes a pick over, for an app that routes it its own way:

```dart
import 'package:flutter/widgets.dart';
import 'package:mountaya_client/mountaya_client.dart';
import 'package:mountaya_elements_flutter/mountaya_elements_flutter.dart';
import 'package:mountaya_geo/mountaya_geo.dart';

class TrailMap extends StatefulWidget {
  const TrailMap({super.key, required this.mountaya});

  // A client whose session token the Data API takes.
  final Mountaya mountaya;

  @override
  State<TrailMap> createState() => _TrailMapState();
}

class _TrailMapState extends State<TrailMap> {
  late final MapEngine map = MapEngine(widget.mountaya);
  String? _opened;

  @override
  void dispose() {
    map.close();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) => Stack(
    children: [
      MapView(engine: map),
      Positioned(
        left: 16,
        top: 16,
        right: 16,
        child: SearchControl(
          map: map,
          onSelected: (row) {
            setState(() => _opened = row.title);
            // The highlight the control would draw, drawn by you.
            map.setSearchHighlight(pickTarget(row.payload).mark);
          },
        ),
      ),
      if (_opened case final opened?) Positioned(bottom: 16, left: 16, child: Text(opened)),
    ],
  );
}
```

- `onSelected(SearchRow row)`: the row's `id`, `title`, `payload` (what it stands for: a place, a point of interest, a way, an itinerary, a zone or a collection), `secondary`, `activities`, and `parts`, the title split for the matched text.
- With `onSelected` set, the control moves no camera and draws no highlight: `pickTarget(row.payload)` answers the `SearchPick` it would act on, its `camera` (an extent or a point) and its `mark` (a `SearchHighlight`).
- `map.setSearchHighlight(mark, accent:)` draws the mark; `setSearchHighlight(null)` takes it down. A mark handed no `accent` is drawn in the standard tokens' light accent: pass `themeTokenValue(MountayaScope.tokensOf(context).accent, MountayaScope.schemeOf(context))` to follow your tokens and a dark scheme.

## Build your own search box

`SearchEngine` is the search under the control, with no widget. Build your own list on its snapshots:

```dart
import 'package:mountaya_client/data_schema.dart' as data_schema;
import 'package:mountaya_client/mountaya_client.dart';
import 'package:mountaya_geo/mountaya_geo.dart';

const publishableKey = String.fromEnvironment('MOUNTAYA_PUBLISHABLE_KEY');
const sessionToken = String.fromEnvironment('MOUNTAYA_SESSION_TOKEN');

final mountaya = Mountaya(
  credential: .publishable(key: publishableKey, sessionToken: const .value(sessionToken)),
);
final search = SearchEngine(
  configuration: SearchConfiguration(
    fetch: (input, signal) => mountaya.data.search(
      input: input,
      options: RequestOptions(signal: signal),
    ),
    language: data_schema.Language.fr,
    targets: const [
      data_schema.SearchTarget.places,
      data_schema.SearchTarget.itineraries,
      data_schema.SearchTarget.zones,
    ],
    limit: SearchLimit.of(5),
  ),
);
search.setQuery('refuge');
final settled = await search.subscribe().firstWhere((state) => state.isSettled);
for (final row in searchListState(settled, SearchFilter.places).rows) {
  print(row.title);
}
search.close();
await mountaya.close();
```

- `SearchEngine({SearchConfiguration configuration, String query = '', MapViewport? camera, bool isOpen = false, bool isConfined = false, SearchPause pause = SearchPause.standard, Duration refreshPause})`: `setQuery`, `setConfined`, `setCamera`, `setOpen`, `retry`, `configure`, and the `Engine` shape (`state`, `subscribe()`, `close()`). Its `state` is a `SearchSnapshot`: `query`, `isSearching`, `isSettled`, `hasFallenBack`, `sections`.
- `searchListState(snapshot, filter)` answers what a list shows under a `SearchFilter` (`all`, `itineraries`, `places`, `zones`): `rows`, `failed` sections, `isNothingFound`, `hasFallbackNote`.
- Hand the engine to `SearchControl(engine:)` to draw it with the box; the State that made it closes it in `dispose`.
- In Flutter, `search.asValueListenable()` (an extension `mountaya_elements_flutter` publishes on every `Engine`) reads its state for a `ValueListenableBuilder`; a broadcast stream hands a late listener nothing, so a reader takes `state` first.
- The rules under it are Geo's, named alike in every SDK: `searchQueryChangeOf`, `highlightRanges`, `placeRow`, `poiRow`, `itineraryRow`, `zoneRow`, `pickTarget`.

## Next steps

- [Show an elevation profile](https://docs.mountaya.com/en/sdks/dart/elevation-profile.md): The itinerary's elevation, linked to the map.
- [Controls and panels](https://docs.mountaya.com/en/sdks/dart/controls.md): Zoom, layers, points of interest and units.
