Skip to content
  1. Home

The Hidden Costs of Flutter Plugins: A Production Horror Story

Last month a silent bug started taking down our Flutter app's most active users, and there's a particular kind of dread that comes with watching the Crashlytics dashboard fill up with red. I'm the lead developer. The buck stopped with me.

Flutter makes you feel a bit invincible, because getting pixels on a screen is so easy that you forget the app is still bound by memory, threading and whatever the device can actually handle. Ours had grown into a sprawling thing, and it was finally turning on us.

What followed was a blur of too much coffee, aggressive refactoring and a few moments of real despair. We ended up rethinking pretty much our whole approach to building mobile apps that hold up in production, so this post is part code and part war story.

Everything knew about everything

We'd coupled everything far too tightly. It took me a while to see it. The UI knew far too much about the database, the database knew far too much about the network, and our state floated around in a chaotic global scope where anything could reach it. At one point I leaned back from a 2,000-line widget file and let out a long sigh, which was about all I could do with it.

Unit tests were off the table. Every class dragged a tangle of dependencies along with it, so the moment I tried to mock a repository, the whole inherited widget chain fell over. Development slowed to a crawl, mostly because the team was scared that touching anything would break a feature that already worked.

Writing code for the bad days

I used to assume a team that writes good code ends up with a reliable app. Ours could write slick algorithms and genuinely beautiful animations, and none of that stopped the app from buckling under its own weight once thousands of users were hitting the servers at the same moment, because the structure holding it all up was flawed.

These days I plan for scale we don't have yet, and for the ugly edge cases. I assume the network will drop out halfway through a request, that a frustrated user will tap the same button over and over, and that the OS will kill a background task right when we need it. Then I write defensively everywhere. It feels paranoid right up until the first time it saves a release.

Code review is how we hold everyone (me included) to this. If a pull request slips raw network logic into what's meant to be a presentation widget, it gets rejected, however small the change is. New features take noticeably longer to land because of that rule, and I'd make the trade again, since maintaining the app over the long run got a great deal faster.

We guard one boundary harder than the rest, the one between business logic and UI code. With the two kept apart, a bug tends to stay inside one class instead of leaking across screens. Once that separation is second nature, building Flutter apps that are fast, scale well and hold up stops being a slog and turns into something I actually enjoy.

The stream leak we kept shipping

Streams were where our memory handling was at its worst. You can spot this leak from across the room:


class _MyScreenState extends State<MyScreen> {
  @override
  void initState() {
    super.initState();
    // Huge memory leak! Never canceled!
    myService.dataStream.listen((data) {
      setState(() { _data = data; });
    });
  }
}

Every time someone opened this screen and left again, another subscription stayed alive. Each one kept trying to call setState on a widget that had already been disposed. The fix is pretty basic. Skip it anyway and you'll pay for it later, usually in a crash report that makes no sense.


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();
  }
}

Questions I get about this

What's the biggest mistake junior Flutter devs make?

Cramming business logic, network requests and error handling into one StatefulWidget. They treat the widget like a monolithic controller when it should be a purely reactive piece of UI, and keeping those concerns separate matters more than anything else in this post.

Should a big app on Provider move to Riverpod or Bloc?

Yes. Do it a piece at a time, though, and don't stop development to do it. Build new features on the new architecture only, then move older screens across whenever maintenance takes you into them anyway. That's how you dodge the dreaded rewrite freeze.

How do you track down performance problems on old Android phones?

Run the app in profile mode on a physical device with the performance overlay switched on. I never debug performance in debug mode on a simulator. Watch for big spikes in UI build times, then go straight after unneeded repaints with the DevTools inspector.

What do you do with huge widget classes?

Break them up early. Once a build method passes 50 lines, I pull its visual pieces out into their own stateless widgets, which makes the code easier to read and keeps rebuilds cheaper by narrowing the context scope.

Where we ended up

Once we'd faced how deep the technical debt went, we spent the next several sprints refactoring hard and pulled the presentation layer completely apart from the business logic. Painful, tedious work. Still, nobody could argue with the results. Our Crashlytics error rate fell by more than 80%, the app felt snappy even on older budget Android phones, and morale went up because people could trust the architecture again.

I don't miss those stressful late-night debugging sessions, but I needed them to grow as an engineer, as much as I hate admitting it. Flutter is a tool. How you use it decides whether your app succeeds or fails. We made these mistakes so that, with any luck, you won't have to.

Our refactoring isn't finished either, and I doubt it ever will be (we keep refining things and adapting as new architecture patterns come along). If your own codebase scares you right now, take a deep breath and start pulling it apart, one methodical piece at a time.

Keep learning and keep questioning your assumptions. When you're stuck, the official Flutter performance best practices are worth a read, and the community has usually solved your exact problem already. Keep coding, stay curious, and build something awesome.

Suraj Bhandari

Written by Suraj Bhandari

Thanks for reading! If this article helped you, share it with someone who needs it.

Comments