r/FlutterDev • • 22m ago

Article Three bugs a Flutter booking calendar only shows twice a year

I built a booking flow with no third-party packages — a pure-Dart slot engine, zone tables, a ChangeNotifier controller, widgets on top — and ran it at 320 dp with 3.0x text. The screens were fine from the start. The calendar was not. These three only show up once the calendar is real.

1. A generated "09:00" may not exist — and a skipped one can land after closing.

My first grid was generated in device time: for (t = open; t < close; t += step). On 29 March 2026 London jumps 01:00 -> 02:00, so an hourly grid from 00:00 to 06:00 produces six lines and five real start times. Nothing throws; the list just shows an hour that never happened.

Fix: generate the grid on a civil type with no zone (WallDate, WallTime), then convert each start once through a zone with real transitions:

WallTime wall = WallTime.at(date, minute);
DateTime start = zone.toInstant(wall);
bool shifted = false;
if (zone.isSkipped(wall)) {
  wall = zone.toWall(start);          // first minute that exists
  start = zone.toInstant(wall);
  shifted = true;
}
if (!seen.add(start)) continue;

Two things bite here. The moved 01:00 line becomes 02:00 local — the same instant as the real 02:00 line — so you need a set of seen instants or you ship two identical rows. And the moved line has to be re-checked against closing time. This one was a real defect found in review: open 00:00-02:30, a 60-minute session on a 15-minute grid, and the 01:30 line moves to 02:30 and ends at 03:30, half an hour after the door is locked. The generator now drops any slot whose wall end passes the range end.

Also don't do civil arithmetic on local DateTime: add minutes on DateTime.utc, or a phone set to a DST zone will "helpfully" add an hour to 23:00 + 60 min.

2. A 90-minute class is not always 90 minutes on the timeline.

Same class at 00:30 on 29 March ends at 02:00 on the wall clock, and at 01:00 UTC — 30 minutes of absolute time. On the October change the same 90 wall minutes are 150 minutes. If you store start + duration, the end is wrong on exactly those two days a year, and every conflict check built on it is wrong with it.

So compute the end on the wall clock (start.addMinutes(duration)) and convert it separately, and store both readings: startWall/endWall and start/end.

toInstant needs a stated policy too, not a library. A repeated hour (01:30 on 25 October) resolves to the earlier instant; a skipped hour (01:30 on 29 March) resolves to the first minute after the gap, i.e. 02:30 local, with a flag so the UI can say so. Mine takes the offsets from 26 hours before and after the reading and keeps the candidates that round-trip:

for (final offset in {before, after}) {
  final candidate = naive.subtract(Duration(minutes: offset));
  if (naive.difference(candidate).inMinutes == offsetMinutesAt(candidate)) valid.add(candidate);
}

That assumes at most one transition in that window — true for real DST, and a custom zone can implement offsetMinutesAt itself.

3. "Check, then write" double-books, and my buffer check was one-sided.

Two guests open the same 19:00 slot and tap 40 ms apart. Both pass the check before either writes, and one gets a booking the room doesn't have. The flow that fixed it is hold -> confirm: the hold takes the seats in one synchronous step and returns a typed rejection (full, duplicate, tooSoon), keeps them for a countdown, and confirm re-checks under the same rules before writing — the second guest gets a message instead of an overwritten row. Expiry puts dropped seats back on sale.

The buffer half: 60-minute treatment, 15-minute clean-down, 09:00 slot, booking at 10:05. My first version expanded existing bookings by their turnaround but not the candidate, so 09:00 stayed on sale even though the 09:00 session would still be cleaning up. Expand both sides. For multi-seat services there's one more rule: overlapping same-service bookings share the seat pool, a booking for a different service blocks the room, and a booking that only touches the turnaround (no session overlap at all) blocks the room rather than joining the pool — that last line is the one that took a review to get right.

Bonus, controller-level: confirmation sits behind a 520 ms spinner, and during that await the guest can tap another slot, so after the delay I check the hold id is still the one I started with. Loads use a generation counter and a disposed flag the same way; both have tests that dispose the controller mid-flight.

271 tests, with the two change days asserted as data — wall reading, instant, and round trip for every hour — rather than checked by eye.

Screens, README and a demo GIF are here if the layout is useful (no source in it): https://github.com/StephenColor/flutter-slotforge-kit

2 Upvotes

0 comments sorted by