The bug that froze Suspect Sketch on “Comparing…”
Suspect Sketch worked on phones but got stuck forever on the web. The culprit was one number that the web compiler quietly turned into zero.
Suspect Sketch is our drawing game: three witnesses describe a suspect, you draw the portrait, and the game compares your sketch with reference images. On iPhone and Android it worked. On the website, pressing Conclude showed “Comparing…” and then nothing happened, ever. The button never came back, so the case could not be finished.
Finding it
The first step was simply playing the game the way a visitor would, inside the caduma.dev player, on a phone-sized screen and on a desktop. The freeze happened every time, on every case, and also on the live site. That ruled out a slow comparison: the comparison itself takes well under a second. Something was failing before it even started.
Every attempt gets a random ID so that the result of an old attempt can never overwrite a newer one. The ID was created like this:
Random.secure().nextInt(1 << 32)In Dart on a phone, 1 << 32 is 4,294,967,296, a perfectly good upper limit for a random number. But Flutter on the web compiles Dart to JavaScript, and JavaScript shift operators work on 32-bit integers. Shifting 1 by 32 bits wraps around to 0, and the compiler folds the constant at build time. The published JavaScript literally asked for a random number below zero.
Asking for a random number below zero throws an error. That error happened before the code that resets the button, so the game was left waiting for a result that would never arrive.
The fix
The upper limit is now a number that fits comfortably in 32 bits on every platform, with a comment explaining why. We also moved everything that could fail during the comparison inside the error handling, so even an unexpected error now gives the button back instead of freezing the screen.
Random.secure().nextInt(0x7fffffff)What else changed while we were there
Once the game could finish a case on the web again, we played a lot of it and fixed what got in the way:
- No lives on the website. The app uses rewarded ads to restore lives, which the browser version does not have, so three escapes used to lock you out for good. On the web you can now retry as often as you like, and the extra hint is free.
- A layout for every screen: three columns on a computer (clues, drawing sheet, tools), a side-by-side view on a phone held sideways, and a portrait phone layout where the Conclude button is always visible.
- Thirteen colours plus a colour wheel, smoother strokes, and keyboard shortcuts for every tool.
- Stars for each case, a case archive with thumbnails that keeps your best attempt, and a button to download your portrait as an image.
The lesson
Code that compiles to several platforms is not guaranteed to behave the same on all of them. Numbers are a classic trap when one of those platforms is JavaScript. We now check web builds by actually playing them in the same player visitors use, not only by checking that they compile.