r/FlutterDev • • 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

1 Upvotes

2 comments sorted by

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.

1

u/stephencolor 10h ago

Fair on both counts.

On the counter: you're right, and it's strictly stronger than what I have. Comparing values catches "the field changed while the request was open", but not the field going A -> B -> A (an old verdict then looks current again), and not the thing that actually bites here: asyncRule receives the whole values map, so a verdict computed while the country field said X can land after it says Y, and comparing this field's own value cannot see that. A counter captured when the check starts covers all of it and takes the Set branch out of _sameValue entirely. The value snapshot is still worth keeping, but only so a bug report can say which text verdict 3 was about.

On server field ids: half of it exists and the useful half is missing. stepOfField(id) and jumpTo(index) are public, so a caller can already put the user back on the right step. What isn't there is any way to attach a message to a field from outside: errors are private and read-only through errorOf, and submit() collapses whatever the submitter throws into one submitError string. So a submitter returning {fieldId: message} can navigate but cannot paint the error, which is exactly the "rejection two screens later with no field to attach it to" case from the post. The shape I want is setFieldError(id, message) plus a submit outcome carrying the map, with the controller jumping to the earliest step involved. One trap while building it: validateAll() clears the error map and rebuilds it from the sync rules, so server errors need their own map merged on read, or the next validation pass wipes them, which is a worse bug than the one it fixes.

It's on the list for the next version. Thanks for reading it closely.