Skip to content
  1. Home

Why Riverpod Saved My Sanity on a Massive Flutter E-Commerce App

Most of what a lead mobile developer does has nothing to do with beautiful animations or clean code. A lot of it is staring at a stack trace at 2 AM, trying to work out why an app that ran perfectly yesterday is failing at random on staging today. That was my last week. I spent it wrestling with our main Flutter app.

The launch had gone well, with rave reviews at first. Under the hood, though, I knew we'd cut corners, and the technical debt had been compounding quietly the whole time, waiting for the worst possible moment to come due. Sure enough, it picked the run-up to a massive marketing push to start showing cracks.

I started questioning our whole tech stack, right down to whether picking Flutter had been a mistake in the first place. Were we just incompetent? It took a lot of trial and error to get to the real answer, which was messier than either of those. Flutter had been doing its job all along, and it was our approach to using it that was broken at a fundamental level.

Where the debt was hiding

Bit by bit I realized how tightly we'd coupled everything. The UI knew too much about the database, which in turn knew too much about the network, and our state floated loose in a chaotic global scope. At one point I sat back from a 2,000-line widget file and just sighed.

Writing unit tests wasn't even possible, because the dependencies were knotted together so badly that mocking a single repository brought the whole inherited widget chain down with it. Development crawled. Developers were afraid that any change they made would break a feature that already worked.

The boundaries we enforce now

If you'd asked me before all this, I'd have said a team of strong coders doesn't ship unreliable software. Our app had slick algorithms and some lovely animations, and it still sagged under its own weight once thousands of users were hitting the servers at once, because the structure underneath was flawed.

Planning now starts with future scale and the nasty edge cases. I assume the network will fail at the worst moment, that an impatient user will tap the same button rapidly and repeatedly, and that the OS will kill background tasks we depend on, and I write defensive code everywhere because of it.

Code review enforces it. A pull request that sneaks network logic into a presentation widget gets rejected. Features take noticeably longer to ship early on, while maintenance over the long run has gotten dramatically faster, and that's the part I care about.

Above all, business logic stays isolated from UI code. That keeps every bug confined to one side of the line, where it's easy to track down. Once that habit settles in, building fast, scalable, solid Flutter apps gets genuinely enjoyable.

Recomputing a list on every rebuild

Here's the kind of heavy, badly optimized build method we were writing back then:


@override
Widget build(BuildContext context) {
  // Doing intense calculations directly inside build!
  final processedList = heavyListProcessing(rawData);

  return ListView.builder(
    itemCount: processedList.length,
    itemBuilder: (context, index) {
      return HeavyWidget(data: processedList[index]);
    }
  );
}

It killed our frame rate. Every minor state change kicked off the expensive calculation all over again, so we moved that work out of the build method.


// The optimized approach
@override
void initState() {
  super.initState();
  // Calculate once, or run in an isolated compute()
  _processedList = heavyListProcessing(widget.rawData);
}

@override
Widget build(BuildContext context) {
  return ListView.builder(
    itemCount: _processedList.length,
    itemBuilder: (context, index) => OptimizedWidget(data: _processedList[index]),
  );
}

One caveat with this version. initState only runs once, so if rawData can change while the screen is open, you need to redo the processing in didUpdateWidget too. And if the list is big enough to stall the UI even once, push the work to a background isolate with compute() and show a loading state while it runs.

Then the small stuff

With the big structural changes in place, we went looking specifically for micro-optimizations. We added const constructors wherever we could, moved heavy JSON parsing into background isolates and cleaned up how assets were rendered. The app went from noticeably laggy to a silky smooth 60 frames per second.

The stakeholders were blown away by how responsive the rewritten app felt when we demoed it. For the technical team that was a massive win, and it validated every one of the difficult calls we'd made getting there.

Questions other Flutter devs ask me

Should a large Provider app move to Riverpod or Bloc?

Often, yes, though only if Provider is actually causing you pain, since it still works fine for plenty of apps. If you do migrate, go incrementally and don't halt development. Use the new architecture exclusively for new features, then move older screens over as you touch them for maintenance, which keeps you out of the dreaded rewrite freeze.

What's the safe way to pass data between screens that aren't connected?

Don't push complex objects that need to change through navigation arguments. Put them in a global state manager or a shared repository service that holds the single source of truth, and have both screens listen to that shared state.

What's the biggest mistake junior Flutter devs make?

Cramming business logic, network requests and error handling into a single StatefulWidget. That treats the widget as a monolithic controller when it should be a purely reactive visual element, and separating those concerns matters more than almost anything else.

Do you still run into memory leaks?

Rarely, but yes. During major refactors I keep the memory view in DevTools open constantly to make sure heavy screens actually get garbage collected after they're popped. You can't stop watching for them.

So, was Flutter the wrong choice?

No. Looking back, those stressful late-night debugging sessions were what made me a better engineer, as unpleasant as they were. Flutter is a tool. How well you use it decides whether you succeed or fail. We learned that through a lot of painful mistakes, and I'm hoping this saves you a few of your own.

The refactoring isn't over, and it probably never will be, since there's always something to refine and some new architecture pattern to adapt to. If your codebase is scaring you right now, take a deep breath and start breaking it apart methodically.

Keep learning and keep questioning your assumptions. When you get stuck, search Stack Overflow's Flutter tag, because the community has usually solved your exact problem already. Keep coding, stay curious, and build something awesome. Ideally something that won't wake you up at 2 AM.

Suraj Bhandari

Written by Suraj Bhandari

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

Comments