From React Native to Flutter: The Brutal Truth About Cross-Platform Development
One short async function caused hundreds of phantom crashes in our Flutter app last month. I didn't know that yet when the Crashlytics dashboard started filling up with red, and if you've ever felt that particular dread, you know exactly what it's like. A silent bug was taking down our most active users, and as the lead developer, the buck stopped with me.
Flutter gives you a false sense of invincibility. Painting pixels on a screen is so effortless that you forget the app still answers to memory limits, threading and whatever the device can actually handle. Ours had grown into a sprawling monster of an app, and it had finally turned on us.
Those next few days blurred together into too much caffeine, aggressive refactoring and the odd moment of pure despair. By the end we'd rethought our whole approach to building mobile apps that hold up, which is why this is as much a production survival story as it is a post about code.
How bad the code had gotten
We'd been misusing Flutter, and badly. Screens were stuffed with massive initialization blocks, heavy API calls and complex data parsing, all mixed right into the layout code, so whenever the product team asked for a minor UI tweak, some completely unrelated network request would break. Our codebase had become dense, tangled spaghetti that nobody could read anymore.
I spent hours planting print statements all over the lifecycle hooks, then watched in horror as the terminal flooded with disposal errors and null-check errors. We'd been passing BuildContext straight into async operations without ever checking that the widget was still mounted. That was a time bomb waiting for production, and production is exactly where it went off.
The function behind the phantom crashes
This is the disaster we started with:
// The absolute nightmare way we used to handle things
Future<void> performAction(BuildContext context) async {
try {
showLoading(context);
final result = await apiService.fetchData();
Navigator.pop(context); // BOOM. Crash if popped early.
Provider.of<StateStore>(context, listen: false).update(result);
} catch (e) {
print('Error: $e');
}
}
That one snippet was responsible for hundreds of phantom crashes. You can't trust a context after an await, because by the time the Future finishes the user may already have left the screen. A context.mounted check patches over it, but we wanted a structural fix, so the logic moved out of the widget and into a notifier.
// How we handle it safely now
class SafeActionNotifier extends StateNotifier<AsyncValue<Data>> {
SafeActionNotifier(this._api) : super(const AsyncLoading());
Future<void> performAction() async {
state = const AsyncLoading();
try {
final result = await _api.fetchData();
state = AsyncData(result); // Safe UI update
} catch (e, st) {
state = AsyncError(e, st);
}
}
}
A note if you're on a current version of Riverpod: StateNotifier is treated as legacy there now, and AsyncNotifier is the recommended way to write this same pattern. The idea doesn't change. The UI watches state and never touches a context after an await.
What I insist on now
I used to trust our team's coding skill to carry the app. We had slick algorithms and properly beautiful animations, and it still started sinking under its own weight once thousands of users were hitting our servers at the same time, because the structure underneath was flawed.
So now I plan for future scale and the brutal edge cases from the start. The network will fail at the worst moment. Some user will tap the same button over and over in frustration. Background tasks we needed will get killed by the OS, and the only sane response to all of it is defensive code everywhere.
Strict code reviews keep us honest about this. Network logic that sneaks into a presentation widget gets the pull request rejected, every time. It slows initial feature development down noticeably, though in exchange long-term maintenance has become drastically faster.
Most of all, business logic stays isolated from UI code, which keeps bugs isolated too. Get that habit into your bones and building fast, scalable Flutter apps that hold up turns into genuinely rewarding work.
Several sprints later
Once we admitted how deep the technical debt went, we spent the next several sprints refactoring aggressively and decoupled the presentation layer from the business logic entirely. It was a painful, tedious stretch. Nobody could argue with the results, though. Our Crashlytics error rate dropped by more than 80%, the app felt snappy even on older budget Android devices, and the team's morale picked up because they could finally trust the architecture again.
FAQ
How do you pass data safely between screens that aren't connected?
I don't pass complex objects that need to mutate through navigation arguments. A global state manager or a shared repository service holds the single source of truth, and both screens listen to that shared state.
What's the biggest mistake junior Flutter devs make?
They cram business logic, network requests and error handling into a single StatefulWidget, treating it like a monolithic controller instead of a purely reactive visual element. Separation of concerns comes first, always.
How do you debug performance on old Android phones?
On a physical device in profile mode, with the performance overlay turned on, and never in debug mode on a simulator. Look for massive spikes in UI build times, then hunt down unneeded repaints right away with the DevTools inspector.
Do you still get memory leaks?
Rarely, but it happens. During major refactors I keep the DevTools memory view open constantly, checking that objects are actually garbage collected after heavy screens are popped. You can't stop paying attention to it.
Still refactoring
Those late-night debugging sessions were incredibly stressful, and I grew more as an engineer during them than I'd care to admit. Flutter is a tool. How you wield it decides whether you succeed or fail, and we learned that the expensive way so you hopefully won't have to.
Refactoring never really ends, either. You keep refining the approach and adapting to new architecture paradigms. If your codebase terrifies you right now, take a deep breath and start breaking it apart methodically.
Keep learning and question your assumptions. When you're stuck, look through Stack Overflow's Flutter questions, since the community has usually solved your exact problem already. Keep coding, stay curious, build something awesome, and may your Crashlytics dashboard stay boring.
Comments
Post a Comment