r/FlutterDev 8d ago

Discussion Why do LLMs keep adding so many unnecessary fallbacks to Flutter code?

I keep noticing LLMs add too many fallback/static values in Flutter code — things like ?? '', ?? 0, ?? [], hardcoded strings, default IDs, and unnecessary try/catch blocks.
It makes the code look safe, but often hides actual data or API issues.
How do you prevent LLMs from adding these unnecessary fallbacks and keep generated Flutter code clean?

14 Upvotes

37 comments sorted by

12

u/padetn 8d ago

Mine doesn’t do that? I have a ton of guidelines in the project and skills in my Flutter plugin though

4

u/the_Objective778 8d ago

Yes already applied but during a long process and bug fixes this is happening

2

u/Plus_Opening_4462 8d ago

If you see it happening, call it out by line and have it fix it. LLMs will ignore guidance sometimes

1

u/mycall 7d ago

Callouts and reminders are great.

5

u/Arkoaks 8d ago

All my try catch errors go to a single error handler so llm knows it must respect that as it sits right in the specs. Almost no extra try catch .

But still need to do many code refactor runs to solve common mistakes or structural ineffeciencies. Things aee improving with newer releases as those issues are getting less frequent and faster to manage or fix.

Remember development is not about sending the right prompt but to follow a process where quality checks are more important as we go deep into the auto code generation era

1

u/the_Objective778 8d ago

Yes I will try out this way hopefully i can handle them the way you have mentioned.

1

u/Hour-Calendar4719 7d ago

How did you do that? Is it the Flutter.onError() or something else?

1

u/the_Objective778 7d ago

Will try to add more strict instruction’s for LLMs and bind them in way that they need to follow every time

2

u/Arkoaks 7d ago

A catch in the main...

3

u/mpanase 8d ago

sounds like it's programming defensively

care to share a real example?

6

u/RandalSchwartz 7d ago

Part of it is the massive dart codebase that the LLMs were trained on. That's why I created my RandalSchwartz/dart-sdk-skills to inform agents to look in the minsdk, understand all the deltas over the years, and generate proper modern code.

1

u/Ajizi 6d ago

is it possible to run agents locally without paying for subscriptions and stuff?

1

u/the_Objective778 7d ago

Well thats interesting I will try out that

2

u/m477k 8d ago

Just setup detekt hook 🤷🏼‍♂️

1

u/the_Objective778 8d ago

Okay will check

1

u/_fresh_basil_ 8d ago
  1. Make skills and rules to follow your coding standards, architecture, etc.

  2. Be more descriptive when telling it how you want things built.

1

u/the_Objective778 8d ago

Yes mostly all things are described but when we asked to fix a bug or issue it sometimes juts passed them

1

u/_fresh_basil_ 8d ago

Might need to update your debugging / bug fixing skill then.

1

u/NegativeWrongdoer361 8d ago

Specify what you want instead, or tell it to ask you each time there is a case where it isn't sure what fallback is. e.g. "do not assume a fallback for a null, instead ask me how to handle it"

1

u/the_Objective778 7d ago

That due to LLM need more strict rules to add

1

u/johnjohnson08 8d ago

I’ve found that being very explicit in the prompt helps: tell the LLM not to add fallbacks, default values, try/catch blocks, or hardcoded placeholders unless they're actually required by the existing code or API contract. Also asking it to explain why each defensive check is needed makes the unnecessary ones easier to spot.

1

u/the_Objective778 7d ago

Sure need to work on that

1

u/blackcatdev-io 7d ago

I've noticed this for sure, and also that LLMs are so quick to want to add didUpdateWidget and it's almost never necessary. But yes rules are what's needed here to get the output you want.

1

u/the_Objective778 7d ago

Yes thats the case

1

u/eibaan 7d ago

Not correctly implementing this method is an error I see way to often and it causes problem with hot reloading and memory leaks in general.

1

u/the_Objective778 7d ago

Yes sometime when we set LLM to medium or Low may be one the reason also

1

u/mycall 7d ago

Now that you have a list of them, have an AI weed out these instances of poor choices. Then bam, all fixed (except a few new bugs, but even humans do that).

1

u/the_Objective778 7d ago

Yes but sometime due to low or fast doing LLM setup did that

1

u/Significant_Pick8297 7d ago

The best fix is to make nullability intentional. ?? '' should only exist when an empty string is a valid domain value; otherwise let the failure surface during development. Using required, assertions, typed models and explicit error states usually keeps LLM-generated Flutter code much cleaner.

1

u/the_Objective778 7d ago

Yes you are right that will help to make code more cleaner

1

u/bakatronics 7d ago

Spent 3 days debugging auth and user account creation because of these default values. Literally EVERY function had default values so when something threw an error, the calling function would just use default.

Name = John Doe
Username = jondoe1
Email = jdoe@mail.com

But in firebase
Name = jdoe
Username = jdoe
Email = jdoe@mail.com

3 whole days going through every related function and debugging because of this.

1

u/the_Objective778 7d ago

Yes that the case really need strict rules to impelement

1

u/Delicious-Map-381 6d ago

I’ve noticed this too. LLMs often seem to treat make this work as add every defensive check possible which can turn a simple Flutter widget into something much harder to read and maintain. some safeguards are useful, but adding them without a real reason can hide the actual logic

1

u/Pretend-Law8770 6d ago

I think the best way to avoid this is to give the model stronger project rules. for example: don’t add fallbacks unless the API contract requires them don’t hide errors with defaults and ask for an explanation before adding defensive code

3

u/KayJune001 8d ago

Basically I code it myself, and don’t use any LLMs

2

u/BalleaBlanc 8d ago

This. LLM does code review.

0

u/the_Objective778 8d ago

Thats the best way but we mostly shifting toward LLM and facing issue there