Writing

Four checks that keep a Flutter app and its API from drifting apart

How I test the seam between a Flutter app and its FastAPI backend: a written contract, recorded responses, backend shape checks, a live contract test and a diff tool.

· 2 min read· flutter, testing, api, fastapi

The worst app–API bugs are not crashes or 500s. They are a renamed field, a fraction that became a percentage, an error body the app does not understand. Both sides pass their own tests; the seam between them has none.

In a side project of mine, a Flutter app with a FastAPI backend, I put four checks on that seam. The backend started as a mock written in Dart and was later ported to Python, so the seam was impossible to ignore.

First, write the contract down

One short document both sides follow, covering what code generators miss:

  • Every error has the same shape: {"error": {"code": "...", "message": "..."}}, with a message written for users.
  • 401 signs the user out. 404 also covers ids the caller does not own, so ids cannot be probed. 422 means a value cannot be computed yet.
  • Percentages are 12.5, not 0.125. Dates are ISO-8601 in UTC.

1. Widget tests on recorded responses

Screens are tested against 206 real responses recorded from the backend, served by a fake client, with a fixed clock so dates stay stable:

final client = MockClient((request) async {
  final fixture = fixtureFor(request); // e.g. holdings_detail.json
  return http.Response(jsonEncode(fixture.body), fixture.status,
      headers: {'content-type': 'application/json'});
});

Fast and offline, but only as current as the recordings. Change the backend and these keep passing.

2. Backend tests that read like the Dart parser

So the backend checks the same shapes from its side: for each payload, a test asserts every field the Dart parser reads is present, with the type the parser casts to.

def check_summary(m: dict) -> None:
    for key in ("id", "name", "short", "value"):
        assert isinstance(m[key], str), key
    assert math.isfinite(m["valueRaw"])

Each test points at the Dart file it mirrors, so when one changes, the other is one click away.

3. A live test, using the app’s own code

The first two checks each test one side against a description of the other. This one removes the description. Pointed at a running backend, it builds the app’s real dependency graph, with the same HTTP client, parsers and error mapping the screens use, and walks a fresh account through every endpoint, then deletes it.

4. A diff between two backends

A script creates the same account and trades on two running backends, say staging and my branch, and compares the key and type tree of every response. Its exit code is the number of differences, so zero means parity and it can gate a deploy.

What each one catches

Check Catches Misses
Recorded responses Parser and UI bugs, fast and offline Any backend change
Backend shape checks Backend changes that would break the parser Fields nobody listed
Live test Real drift, end to end Anything off the scripted path
Two-backend diff Regressions between deployments Bugs present in both

None is enough alone. Together they let me change either side without opening the other.

Written by Advait Bhitkar, mobile engineer. If this was useful, or wrong,email me.