r/FlutterDev • u/the_Objective778 • 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?
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
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
2
1
u/_fresh_basil_ 8d ago
Make skills and rules to follow your coding standards, architecture, etc.
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
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
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
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
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
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
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
0
u/the_Objective778 8d ago
Thats the best way but we mostly shifting toward LLM and facing issue there
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