The Backend-to-UI Model Bridge Pattern
In a single-tenant app, it’s tempting to let a UI model match the backend response field-for-field — less mapping code, one less layer to maintain. In a multi-tenant platform, that shortcut turns an ordinary backend change into a forced app release.
The Problem With a 1:1 Model
If a Flutter model is hard-coded to a backend field name, a backend rename breaks it immediately.
Illustrative example: a repository-level model expects a categories field. The backend renames it to shop_categories for a new tenant’s data shape. fromJson now returns null (or throws) for that field, and any view reading model.categories either shows nothing or crashes — and because the model and the UI are the same shape, there’s no single place to patch; the fix has to ship as a new app build.
The Fix: Map at the Repository Boundary
Instead of letting backend shape leak all the way to a widget, the repository layer maps the raw response into a stable UI-facing model. The widget never sees the backend’s field names at all — only the repository’s output shape.
// Illustrative — the repository is the one place backend-shape knowledge lives.
if (sectionType == 'CategorySection') {
return CategorySectionModel(
// Map whichever field name this response actually used into the one
// name the UI model always exposes.
images: json['picture_urls'] ?? json['images'] ?? [],
title: json['title'],
);
}
When the backend renames a field, exactly one mapping line changes. The widget consuming CategorySectionModel.images is untouched — no new release required for that kind of change.
This is the same job this app’s own layering already assigns to the Repository and Service layers (see ui-application/CLAUDE.md’s Feature Triad): the Repository is the bridge for data — raw JSON in, a stable domain model out — and the Controller/Service layer is the bridge for state, turning that model into what a screen actually renders (loading, empty, error, populated). Neither layer should assume the backend’s wire format and the UI’s model shape will always match.
Why This Matters More for a Multi-Tenant App
A multi-tenant backend iterates fast — a new tenant’s data shape, a new optional field, a differently-ordered response — while the installed app can’t be forced to update on the same schedule; some users are always on an older build. A model that tolerates an unrecognized or missing field (defaulting to an empty value instead of throwing) keeps an older app build working against a backend that has since moved on, instead of crashing on anything it wasn’t built to expect.
The trade-off to watch: this flexibility is only safe as long as the mapping layer stays disciplined about defaults — a field that’s silently defaulted to empty instead of surfaced as an error can also hide a real bug. Treat an unexpectedly-empty mapped field as worth checking, not just worth tolerating.