← All languages

Servirtium for

Dart (and Flutter)

No shipped package: vendor the binding's source at a release tag, with the native library from the release. Release v2.0.0-alpha.20.

FFI mechanism
dart:ffi. A pub package that opens the native library with DynamicLibrary.open (dart:ffi, plus package:ffi). The same binding serves Flutter, but the library must be reachable by the host process that runs the tests (host-VM flutter test, not a device).
Toolchain
Dart SDK ^3.8.0 (pubspec.yaml; the repo's own example uses ^3.12.0); package:ffi ^2.1.0; run was on Dart 3.13.7
Distribution
No pub.dev package: pub git dependency on the release tag (path: dart), plus the release native library
Finding the library
SERVIRTIUM_VCR_LIB (absolute path to an existing file), or nativeLib: on Vcr.playback / Vcr.record. Set it in whatever process runs dart test / dart run.
On GitHub
dart/ · README

Getting it

There is no published package for Dart (and Flutter). You vendor the binding's source into your project at the release tag v2.0.0-alpha.20 and take the native library from the same release.

  1. Fetch the native library for your OS/arch from the release into a directory outside your git tree and verify the checksum.
    REL=https://github.com/servirtium/servirtium-vcr/releases/download/v2.0.0-alpha.20
    LIBDIR="$HOME/.servirtium-vcr/v2.0.0-alpha.20"; mkdir -p "$LIBDIR"
    LIB=libservirtium_vcr-v2.0.0-alpha.20-macos-arm64.dylib      # or -linux-x86_64.so, -linux-arm64.so, ...
    curl -fsSL -o "$LIBDIR/$LIB" $REL/$LIB
    curl -fsSL -o "$LIBDIR/$LIB.sha256" $REL/$LIB.sha256
    ( cd "$LIBDIR" && { sha256sum -c $LIB.sha256 || shasum -a 256 -c $LIB.sha256; } )
    
  2. Add the package as a git dependency on the tag. path: dart is needed because the repository root has no pubspec.yaml.
    dart pub add servirtium --git-url https://github.com/servirtium/servirtium-vcr.git --git-ref v2.0.0-alpha.20 --git-path dart
    
  3. Or in pubspec.yaml directly.
    dependencies:
      servirtium:
        git:
          url: https://github.com/servirtium/servirtium-vcr.git
          ref: v2.0.0-alpha.20
          path: dart
    
  4. Point the package at the library wherever dart test / dart run runs.
    export SERVIRTIUM_VCR_LIB="$HOME/.servirtium-vcr/v2.0.0-alpha.20/libservirtium_vcr-v2.0.0-alpha.20-macos-arm64.dylib"
    dart test
    

Record

Point your client at the VCR's base URL; the VCR forwards to the real service and hands back the real response. Nothing is written until you commit.

import 'package:servirtium/servirtium.dart';

final vcr = Vcr.record('tapes/climate_api.md', 'https://climatedataapi.worldbank.org')
    .port(0)
    .start();
try {
  // drive the SUT against vcr.baseUrl ...
  vcr.commit();      // the only thing that writes the tape
} catch (_) {
  vcr.discard();     // drop the half-finished recording
  rethrow;
}
// close() without commit() or discard() writes nothing and throws.

Playback

The same tape, replayed with no network.

import 'package:servirtium/servirtium.dart';

final vcr = Vcr.playback('tapes/climate_api.md').port(0).start();   // 0 = OS-assigned
try {
  // point the SUT at vcr.baseUrl, drive it ...
  expect(vcr.lastKind, VcrOutcome.ok);                              // optional: assert a clean match
} finally {
  vcr.close();
}

Idiomatic extras

Discard on the failure path

discard() drops the recording and writes no tape; after it, close() is a no-op.

final rec = Vcr.record(tape, upstream).port(0).start();
try {
  await drive(rec.baseUrl);
  rec.commit();
} catch (_) {
  rec.discard();
  rethrow;
}

Redact credentials and JSON fields at record time

Applied when the tape is written; the live test still sees real bytes. A scrubbed tape replays with redactCredentials() on the playback builder too.

Vcr.record(tape, upstream)
    .redactCredentials()
    .redactJsonField('api_key', field: VcrField.responseBody)
    .port(0)
    .start();

Vcr.playback(tape).redactCredentials().port(0).start();

Drift detection

With failIfChanged, commit() writes the fresh tape and throws VcrException if it differs from the one on disk.

final rec = Vcr.record(tape, upstream).failIfChanged().port(0).start();
// ... drive the SUT ...
rec.commit();   // throws VcrException on drift

Gotchas

  • Use ref: set to a tag, not main: the library must be at least as new as the Dart source.
  • Do not write servirtium: ^2.0.0 or dart pub add servirtium with no --git-*; it is not on pub.dev. Do not omit path: dart.
  • Do not put the library in the pub cache (~/.pub-cache/git/...): it is a cache.
  • Flutter: the library must be reachable by the process running the tests (host-VM flutter test, not a device) and the path must be absolute.
  • Dart's HttpClient sends default request headers; under strictHeaders() they must be on the recorded block or removed with removeHeader.
  • Windows: take the .dll asset and give SERVIRTIUM_VCR_LIB its full path.

Prompt for your coding agent

Paste this into Claude, Codex, Gemini or any coding agent, in your Dart (and Flutter) project. It wraps one of your existing integration tests with Servirtium. The general prompt has a slot for the language instead.

Hey Gemini or Claude or Codex,

I want a test in this Dart (and Flutter) project to run without calling a real web service every time. Use Servirtium (https://servirtium.dev), which records HTTP conversations as readable Markdown "tapes" and replays them. Do these steps in order and tell me what you did at each one.

1. Find an existing integration test in this project that calls a web service as a side effect of what it is testing. Prefer one that is slow, flaky, or needs credentials or network because of that call. Tell me which test you chose and why.

2. Add the Servirtium binding for Dart (and Flutter). Follow https://servirtium.dev/languages/dart/ exactly. Servirtium ships no package for this language. Vendor the binding's source into the project at the release tag v2.0.0-alpha.20 and put the native library from the same release where the page says: No pub.dev package: pub git dependency on the release tag (path: dart), plus the release native library.
   Toolchain and minimum version: Dart SDK ^3.8.0 (pubspec.yaml; the repo's own example uses ^3.12.0); package:ffi ^2.1.0; run was on Dart 3.13.7.
   How the binding finds the native library: SERVIRTIUM_VCR_LIB (absolute path to an existing file), or nativeLib: on Vcr.playback / Vcr.record. Set it in whatever process runs dart test / dart run.

3. Route that test's HTTP client to the Servirtium VCR's base URL instead of the real service, using the idiomatic Dart (and Flutter) way of setting a base URL or HTTP client. The VCR starts on its own port (use port 0, so it picks a free one) and gives you its base URL; give that URL to the client and never hard-code a port.

4. Run the test once in record mode against the real service. Commit the recording when the test passes and discard it when the test fails: a record VCR that is closed without a commit or a discard writes no tape and raises an error. Use redact_credentials (the spelling for Dart (and Flutter) is on the page) so secrets become numbered tokens, and remove volatile request headers such as User-Agent and Date from the tape so playback does not depend on them.

5. Keep the tape beside the test as readable Markdown, in version control. Open it and check there are no secrets or personal data in it before committing.

6. Make the test play back by default, with an explicit switch (for example an environment variable such as SERVIRTIUM_RECORD=1) that re-records.

7. Prove it works offline: make the real service unreachable (network off, or a wrong host) and show the test still passes in playback.

Traps to avoid:
- A record VCR needs Commit() or Discard(); closing it without either writes nothing and raises.
- The VCR runs in your test process on its own port; the system under test must use the VCR's base URL, not the real one.
- Use ref: set to a tag, not main: the library must be at least as new as the Dart source.
- Do not write servirtium: ^2.0.0 or dart pub add servirtium with no --git-*; it is not on pub.dev. Do not omit path: dart.
- Do not put the library in the pub cache (~/.pub-cache/git/...): it is a cache.
- Flutter: the library must be reachable by the process running the tests (host-VM flutter test, not a device) and the path must be absolute.
- Dart's HttpClient sends default request headers; under strictHeaders() they must be on the recorded block or removed with removeHeader.
- Windows: take the .dll asset and give SERVIRTIUM_VCR_LIB its full path.

References:
- This language's page: https://servirtium.dev/languages/dart/
- The binding's directory: https://github.com/servirtium/servirtium-vcr/tree/main/dart
- Concepts, features and every language's quick start: https://servirtium.dev/llms-full.txt (index: https://servirtium.dev/llms.txt)

How these snippets were checked

Real run on cachyos (Linux x86_64, Dart 3.13.7), not on the Mac (no Dart SDK installed there; Flutter not tried): dart create + dart pub add servirtium --git-url/--git-ref v2.0.0-alpha.20/--git-path dart, release linux-x86_64 .so (sha256 verified) via SERVIRTIUM_VCR_LIB. The README 'Getting it' check printed 'ok-body VcrOutcome.ok'. A second script recorded against a dart:io upstream with redactCredentials + redactJsonField then commit() (secret and bearer token absent, [REDACTED] present), record then discard() (no tape), and replayed the scrubbed tape with playback redactCredentials() (VcrOutcome.ok). Not run: macOS arm64, failIfChanged, Flutter.

They mirror these files in the repository:

See also the feature reference, llms.txt and the dart/ directory.