# Pagination and safe writes

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

Read a whole listing as a Dart Stream with walk methods and PageWalk, repeat a write safely with an idempotency key, and write only if nothing changed.

A listing is read a page at a time. A write carries a key that makes repeating it safe, and a tag that refuses it once the resource has changed.

## Read a whole listing

Beside each listing stands its walk, `walk` and the operation's name. It answers a `Stream` that asks for each page as you read the rows before it:

```dart
import 'package:mountaya_client/mountaya_client.dart';
import 'package:mountaya_client/rest_schema.dart' as rest_schema;

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)),
);
await for (final itinerary in mountaya.studio.itineraries.walkGetItineraries(
  query: rest_schema.GetItinerariesQuery(
    sort: rest_schema.GetItinerariesSort.updatedAtDescending,
  ),
  pageSize: 50,
  maxPages: 10,
)) {
  print(itinerary.name);
}
await mountaya.close();
```

`PageWalk` is the same walk over any listing whose page method you call yourself.

- A walk takes the listing's typed `query:` (its filters and `sort`, sent on every request), sets each page's `limit` and `cursor` itself, and stops at the last page or at the `maxPages:` you name. It requests no further page once your subscription is cancelled, or a `break` leaves the `await for`.
- `pageSize:` is `PageWalk.defaultPageSize` (50) unless you name another, up to `PageWalk.maxPageSize` (100).
- `PageWalk<Q, R>({required Q query, required page, int pageSize, int? maxPages})`: `Q` is a `PagedQuery` (every printed listing query is one, with `withPage(cursor:, limit:)`), and `page` is a `Future<Response<List<R>, ListMetadata>> Function(Q, AbortSignal)`.
- `PageWalk(query: const rest_schema.GetZonesQuery(), page: (query, signal) => mountaya.studio.zones.getZones(query: query, options: RequestOptions(signal: signal)))` is a `Stream` of the rows.
- `mountaya.walk(OperationDeclaration(...), decodeRow:)` walks a listing this build prints no method for, with headers of your own on every page.
- A walk is withdrawn by `options: RequestOptions(signal: signal)`, and ends with the signal's reason.
- Without a walk, `response.metadata.pagination.next_cursor` holds the next page's cursor, `null` on the last page: pass it back as `query.cursor`.

## Retry a write safely

A write sends one `Idempotency-Key` on every attempt the client makes. Keep your own key with the write, so a retry of yours lands once too:

```dart
import 'dart:math';

import 'package:mountaya_client/mountaya_client.dart';
import 'package:mountaya_client/rest_schema.dart' as rest_schema;

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)),
);
// Drawn once, and kept with the write until it lands.
final random = Random.secure();
final key = [
  for (var byte = 0; byte < 16; byte += 1)
    random.nextInt(256).toRadixString(16).padLeft(2, '0'),
].join();
Future<Response<rest_schema.ItineraryWithDetails, Object?>> create() =>
    mountaya.studio.itineraries.postItinerary(
      body: rest_schema.ItineraryCreateParams(
        activity_id: 'a1c4e7b2-3d59-4f08-8b6a-2e5c9d0f7134',
        is_private: true,
        name: 'Tour du Mont-Blanc',
      ),
      idempotencyKey: key,
    );
try {
  print((await create()).data.id);
} on ConnectionException {
  // No answer came: send it again under the same key, and it is made once.
  print((await create()).data.id);
}
await mountaya.close();
```

- A key you pass is sent exactly as given on every attempt; one you leave out is drawn from the client's `RandomSource` (cryptographic by default) and still sent on every attempt.
- A repeat of a key whose first request is still running is a `409` coded `IDEMPOTENCY_KEY_IN_USE`, asked again while the retry budget lasts, then `IdempotencyKeyInUseException`.
- A repeat whose first request made its write but whose answer cannot be given again is `IdempotentAnswerUnavailableException`: read `failure.answer.location`, and never send the write again under a new key.
- `ResponseReport.isReplayed` tells an answer replayed for a key.
- The key is printable ASCII; anything else is a `ConfigurationException` before the request leaves. Keys and their lifetime: [idempotency](https://docs.mountaya.com/en/apis/conventions/idempotency.md).

## Write only if nothing changed

Pass the tag you read the resource under. A write over a newer version is refused:

```dart
import 'package:mountaya_client/mountaya_client.dart';
import 'package:mountaya_client/rest_schema.dart' as rest_schema;

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)),
);
const itineraryId = '0b6d1a4e-6d3f-4f7a-9c2e-1f0a5b8c3d21';
var read = await mountaya.studio.itineraries.getItinerary(itinerary_id: itineraryId);
try {
  await mountaya.studio.itineraries.patchItinerary(
    itinerary_id: itineraryId,
    body: rest_schema.ItineraryUpdateParams(name: 'Tour du Mont-Blanc, summer'),
    ifMatch: read.data.etag,
  );
} on PreconditionFailedException {
  // Someone changed it first: read it again, then decide.
  read = await mountaya.studio.itineraries.getItinerary(itinerary_id: itineraryId);
}
await mountaya.close();
```

- `ifMatch:` sends `If-Match` exactly as given: the `etag` you read the resource under (`response.etag` or the resource's own `etag` field), or `'*'`. The client never writes a tag you did not name.
- A stale tag is a `412`, `PreconditionFailedException`, carrying the resource's tag now in `etag`. It is never repeated.
- Only operations that declare `If-Match` take `ifMatch:`; it is not a parameter elsewhere, so a misplaced one does not compile.
- Conditional writes and which operations take them: [conditional writes](https://docs.mountaya.com/en/apis/conventions/conditional-writes.md).

## Next steps

- [Manage an organization](https://docs.mountaya.com/en/sdks/dart/admin.md): Members, keys and webhook endpoints, from a server.
- [Errors and retries](https://docs.mountaya.com/en/sdks/dart/errors.md): What a failed call throws.
