Refactor before adding AI when the existing structure prevents you from controlling inputs, identifying results, cancelling work, or protecting the right user's data. A messy folder tree alone is not a reason to postpone every product experiment.
Start by tracing one proposed AI task through the current app. Identify where the request begins, which data it can read, how the result is stored, and what happens if the user navigates away or changes accounts.
Find the boundary that blocks the feature
Look for network calls inside widgets, duplicated account state, unstructured error handling, and results written without a stable operation reference. These are concrete risks because AI work may finish later than the screen that requested it.
Use Flutter's architecture guide as a reference for separating responsibilities. The useful outcome is clearer ownership, not mechanically adding every layer shown in an example.
Scope a fictional request-summary feature
Suppose staff want a suggested summary of a service request. The current detail widget loads records, calls external services, and saves changes directly. Adding another call there would make it difficult to tell whether a late summary belongs to the currently displayed request.
A focused refactor can introduce a named summary operation with request identity, owner, input revision, state, and result. Keep the existing screen while moving data access behind a clear boundary. Require staff review before replacing the original description.
Test navigation to another request while processing, sign-out before completion, edited input, provider failure, and repeated submission. Confirm the suggestion cannot overwrite another record or become accepted merely because generation finished.
Avoid turning a feature repair into a rewrite
Separate necessary work from desirable cleanup. A stable repository boundary and explicit operation state may be enough for the pilot. Replacing the navigation system or state-management package needs its own evidence of value.
Estimate the refactor with acceptance cases: the existing detail path still works, the AI task can fail independently, and result ownership is preserved. Keep a small regression set around the unchanged workflow so the cleanup does not quietly remove existing behaviour.
The tradeoff is near-term delivery versus future control. Skipping the boundary can make the first demo quicker while creating difficult race conditions. Broad architectural renovation can consume the pilot budget before demand is tested. Choose the smallest change that removes the demonstrated risk.
The Social App case study documents Flutter, native, backend, and architecture work. This summary feature is fictional and makes no claim about its code structure or measured results.
Questions about Flutter refactoring before AI
Should old code always be modernised first?
No. Identify a concrete feature or operating risk. Cosmetic age and unfamiliar style do not establish that a rewrite is necessary.
Is a new state-management package required?
Only if evidence shows the current approach cannot express the needed ownership and lifecycle cleanly. Package choice is separate from the boundary decision.
What should the first review deliver?
Deliver one traced operation, failure cases, a bounded refactor, and verification criteria. Bring that scope to a Flutter engineering discussion.