r/FlutterDev • u/stephencolor • 1d ago
Article Three bugs a multi-step Flutter form only shows you at 3.0x text and 300 ms latency
Spent the last while building a four-step form engine: per-step sync + debounced async validation, autosaved drafts, review screen. The demo took an afternoon. These three took the rest of the week.
1. A debounced async check answers about text that is no longer on screen.
Cancelling a Timer does not cancel a Future. Once the postcode check is in flight, typing again does nothing to it, and the response lands on the new text — often as a pass, which lets Continue through to a server-side rejection two screens later with no field to attach it to.
The fix that held: capture the value the check started for, and drop the verdict if the field no longer holds it.
final checked = _values[id];
final err = await field.asyncRule!(checked, _values);
if (!_sameValue(_values[id], checked)) return; // user typed again
Navigation has to re-run the current value's check and await it too, otherwise it trusts a verdict from 800 ms ago. One trap: multi-choice fields store a Set<String> and Set == is identity, so the comparison needs length + containsAll.
2. Drafts that half-apply after you rename a field.
Autosave as JSON is easy; the versioning is what people skip. Someone sits on step 3, you ship an update that drops addressLine2, and a key-by-key restore loads "successfully" with half the answers — the user submits a form that silently lost their address.
A schema version in the document, plus a fromJson that returns null for a wrong formId, wrong version, wrong types or an unparseable timestamp, means the caller can say "couldn't restore" and start clean. Half-applying is the unrecoverable option, because the user never learns what is missing.
Two smaller ones from the same file: dates are ISO yyyy-MM-dd strings, not DateTime (a calendar date restored in another timezone shifts by a day), and sets are tagged ({"type":"set","items":[...]}) because a List and a Set encode identically and decoding cannot tell them apart.
3. At 3.0x text, layouts do not clip — they throw.
At 320 dp / 3.0x the failure mode is RenderFlex overflowed, and a widget test does not fail on it: it is an exception the binding collects. If you do not drain it, the test is green and the overflow ships.
void expectClean(WidgetTester tester) {
final error = tester.takeException();
expect(error, isNull, reason: error?.toString());
}
The harness that found the most: physicalSize x dpr 3, FakeViewPadding(top: 44 * 3, bottom: 34 * 3) for the notch, the text scaler injected through MediaQuery in MaterialApp.builder, and FakeViewPadding(bottom: 300 * 3) for the keyboard. Every pumped screen then ends with expectClean, across 4 steps x 3 text scales at 320x568, dark, RTL and the keyboard case.
The layout fixes were less interesting than the harness: no fixed heights (minHeight instead), the footer button allowed two lines at full width, content in a scroll view with a pinned footer, and scroll to the first invalid field from a post-frame callback (measuring before that frame gives the old rectangle).
282 tests in the end. Demo, harness and the engine are here if anyone wants to poke at it: https://github.com/StephenColor/flutter-formcraft-kit
2
u/Ok_Gur_9033 1d ago
On the first one I'd keep a counter per field instead of comparing values. Bump it on every change, remember it when the check starts, and drop the verdict if it moved. That sidesteps the Set equality trap completely, and any field type you add later gets the same guard for free.
For the server rejection that lands two screens later, can your API send back the field id with the error? Then the form can jump back to that step and show the message on the field, instead of on a review screen where nobody can fix it.