When I first started building Flutter apps, I kept wondering why my counter wouldn’t update when I pressed a button. The variable was changing in the code, but the screen stayed frozen at zero.
I spent hours staring at the screen. I thought I broke the framework.
That’s when I learned about State Management the hard way. If you've ever felt like Flutter is "ignoring" your code, this guide is for you.
The "Aha!" Moment: What is State?
Think of State as your app’s memory. It’s the data that determines what users see right now.
The items in a shopping cart? State.
A checked box? State.
That counter that refused to move for me? State.
Stateless vs. Stateful (The "Photo" vs. "Mirror" Analogy)
On Dev.to, we often see people overcomplicate this. Here is how I finally understood it:
This is the function that changed everything for me. When you call setState(), you aren't just changing a variable; you are screaming at Flutter: "HEY! SOMETHING CHANGED! REBUILD THE UI!"
Dart
`
`
// The mistake I made:
onPressed: () {
counter++; // The variable changes, but the screen stays 0.
}
// The fix:
onPressed: () {
setState(() {
counter++; // Now Flutter knows to repaint the screen.
});
}
`
`
3 Mistakes That Nearly Made Me Quit
Changing variables outside setState(): The most common beginner trap.
Forgetting to dispose() controllers: I caused a massive memory leak in my first project because I didn't clean up my TextEditingController. Always dispose your controllers.
Using setState() for EVERYTHING: It’s great for local state (like a single form field), but if you try to manage your entire app’s login status with it, your code will become "spaghetti."
Ephemeral State vs. App State
In 2026, we have amazing tools like Riverpod and Bloc, but the official Flutter docs make a great distinction that I live by:
Ephemeral State: "I’m only using this data on this screen." (Use setState)
App State: "I need this data everywhere (e.g., User Profile)." (Use a State Manager)
What was the first thing that confused you when learning Flutter? For me, it was definitely the "Stateful vs Stateless" split. Let’s discuss in the comments!
This is an excerpt from my deep-dive series on Flutter. You can find the full, interactive To-Do App tutorial over at Deadloq.
Top comments (0)