Programming with AI
ENRU
11.6

AI mistakes in Flutter, and a checklist

and how to check what I couldn't

What it is

The chapter's closer. And here, something no other chapter has: a list of what you have to check yourself, because I couldn't (article 11.1).

AI mistakes in Flutter

  1. Code for an old version. This stack's main problem. Outdated widget and parameter names; flutter analyze usually complains.

  2. !, late, dynamic. Three ways to switch off the emptiness check — the direct analogue of any, as and ts-ignore from article 9.6.

  3. Assignment without setState. The value changes, the screen doesn't.

  4. No dispose. Controllers and subscriptions outlive the widget.

  5. A network request in build. build is called often.

  6. Stateful with no state. An extra class.

  7. No const where the widget doesn't depend on data. Needless recreation.

  8. An asset not declared in pubspec.yaml. The error comes when the screen opens, not at build time.

  9. Only the happy case is built. No loading, error or emptiness (article 11.5).

  10. A denied permission isn't handled. "Denied permanently" especially.

  11. A second state library added. Because the request didn't name the first one.

  12. Fixed sizes. Overflow on a narrow screen.

A Flutter code checklist

1. The analyzer

flutter analyze

The first and most important step — here it stands in for mypy and tsc. It catches outdated APIs, forgotten consts, unused things, and some of the emptiness errors.

2. Run it

flutter run

Hot reload shows an edit at once, but not every change is picked up: edits in initState and in declarations often need a full restart. If a change "didn't apply", restart completely before looking for a bug in the code.

3. A narrow screen and long text

Run it on a small device or narrow the window. Type in a long name. The yellow-and-black overflow stripe is the commonest finding.

4. The four states

Loading, empty, error, data. For the error, turn off the network on the device.

5. Permissions

Deny the permission. Then deny it permanently. The application has to keep working and explain what to do.

6. Both platforms

If you build for Android and iOS, check on both. The back button, the safe areas and the dialogs behave differently.

How to check this chapter

The examples here weren't run, so — how to satisfy yourself that what is written is right:

Prompt
Here is an extract from a Flutter handbook: <extract>.
I'm on Flutter 3.38. Is this current for my version?
If anything is outdated or inaccurate, say exactly what,
with a link to the documentation.

And the most reliable way is simply to run it. Create an empty project and check whatever looked doubtful:

flutter create test_app && cd test_app && flutter run

What to tell the AI

Prompt
Check this Flutter code against the list:
- is the API current for Flutter 3.38
- are there !, late or dynamic, and why
- does every state change go through setState
- do the controllers and subscriptions have a dispose
- are there requests in build
- is there a const where the widget is static
- are loading, error and emptiness implemented

Which of these are there? If none, say so.

Cheat sheet

  • This stack's main mistake is code for an old version. Name yours.

  • !, late, dynamic are gags, not solutions.

  • Changes only through setState; close controllers in dispose.

  • flutter analyze is the first checking step, standing in for mypy and tsc.

  • "It didn't apply" — a full restart first, then look for the bug.

  • Check: a narrow screen, the four states, a denied permission, both platforms.