The Day My Flutter Animations Tanked Production Performance
It was supposed to be a quick bug fix, and it took exactly four days. What looked like a simple layout problem went all the way down to how we were handling asynchronous streams across different tabs, and it turned out we were rebuilding entire sub-trees of the app sixty times a second for no reason at all.
Being a lead mobile developer involves fewer smooth animations and less clean code than people imagine. Last week, for example, I was staring at a stack trace at 2 AM, trying to figure out why our main Flutter app, which had worked perfectly the day before, was failing at random on staging.
We'd launched to rave reviews, at first anyway. I also knew we'd cut corners getting there. The technical debt had been compounding quietly in the background, waiting for the worst possible moment to collect, and sure enough the cracks started showing right before a massive marketing push.
So I questioned everything, Flutter included. Maybe it had been the wrong choice, or maybe we were just incompetent. A lot of trial and error later, the real answer turned out to be more nuanced than either. Flutter was never the issue, and our whole approach to it was fundamentally broken.
A month with no new features
I sat down with our product manager and said, in so many words, that we had to stop all new feature development for a solid month just to stabilize the app. That's still one of the hardest conversations I've had in this job. We were bleeding performance and user trust, though, and there wasn't another way out.
Habits that came out of it
Before this, I'd have told you pure coding skill was most of what it takes to ship reliable production software. We had the skill. We had slick algorithms and some really beautiful animations, and the app still started to buckle under its own weight once thousands of users were hitting the servers at the same time, because the structure underneath was flawed.
These days, future scale and the brutal edge cases get planned for up front. My default assumptions are a network that fails without warning, a frustrated user tapping one button as fast as their thumb allows, and an operating system that kills background tasks we rely on, so the code gets written defensively everywhere.
Our code review culture is strict on purpose. Anyone who sneaks raw network logic into a presentation widget gets the pull request rejected. Initial feature development is noticeably slower for it, and long-term maintenance is drastically faster, which is a trade I'm happy with.
The line we hold hardest is between business logic and UI code. Keeping those isolated means bugs stay isolated too, in one place you can actually find. Once you're used to working like this, building fast, scalable, solid Flutter apps gets rewarding in a way it never was for me before.
Memory leaks in our streams
Streams were also where our memory management was at its worst. This leak is about as obvious as they come:
class _MyScreenState extends State<MyScreen> {
@override
void initState() {
super.initState();
// Huge memory leak! Never canceled!
myService.dataStream.listen((data) {
setState(() { _data = data; });
});
}
}
A user moving in and out of this screen left a new subscription running every time, and each one went on trying to call setState on widgets that had already been disposed. The fix is basic. You still can't skip it.
class _MyScreenState extends State<MyScreen> {
late StreamSubscription _sub;
@override
void initState() {
super.initState();
_sub = myService.dataStream.listen((data) {
if (mounted) setState(() { _data = data; });
});
}
@override
void dispose() {
_sub.cancel(); // Properly cleaning up!
super.dispose();
}
}
What a few sprints of refactoring got us
Once we'd faced up to how deep the technical debt ran, we spent the next several sprints refactoring hard, decoupling the presentation layer completely from the business logic. It was painful, tedious work, and nobody could argue with the results.
Our Crashlytics error rate dropped by more than 80%. The app felt snappy even on older budget Android phones, and the team's morale went up noticeably, because people could finally trust the architecture again.
Those stressful late-night debugging sessions were miserable at the time, but I needed them to grow as an engineer. Flutter is just a tool, and how you use it decides whether you succeed or fail. We learned that expensively enough that I'd like to spare you the bill.
Refactoring never really finishes. You keep refining your approach and adapting to new architecture paradigms as they come along, and if your own codebase terrifies you right now, take a deep breath and start breaking it apart methodically. Keep learning, keep questioning your assumptions, keep coding, stay curious, and go build something awesome.
Questions I keep getting
How do you debug performance on old Android phones?
Profile mode, a physical device and the performance overlay switched on, every time. Never debug performance in debug mode on a simulator. Look for big spikes in UI build times, and when you see one, go straight after unnecessary repaints with the DevTools inspector.
How do you pass data safely between screens that have nothing to do with each other?
If it's a complex object that will change, I don't send it through navigation arguments. A global state manager or a shared repository service holds the single source of truth, and both screens listen to it.
What do you do with a massive widget class?
Break it up, aggressively. Once a build method runs past 50 lines, I extract specific visual pieces into their own stateless widgets. Readability improves a lot, and because each piece is now its own widget (ideally with a const constructor), Flutter can skip rebuilding the parts that didn't change.
Is moving a big Provider app to Riverpod or Bloc worth it?
Often, if Provider is actually holding you back, though it still works well for plenty of apps. Either way, migrate incrementally and keep development going. New features go on the new architecture only, older screens move over as you touch them for maintenance, and you avoid the dreaded rewrite freeze. Our month without features was a different situation, an emergency stabilization rather than a state-management migration.
If you're weighing that move, Riverpod's page on pub.dev is the place to start, and for everything else you get stuck on, the community has usually solved your exact problem already.
good
ReplyDelete