Thuta Learning
Flutter
BasicMobile DevelopmentBeginner

Stateless vs Stateful

FlutterLesson 720 min

What you'll walk away with

  • Explain the widget, state, and lifecycle behavior of Stateless vs Stateful
  • Test responsive, failure, and interaction cases
  • Write accessible, testable, and performant Flutter

Explain it simply

Stateless vs Stateful, in plain language

Put simply, Flutter widgets can be split into ones that don't change and ones that can. StatelessWidget is used for UI whose data/state never changes, while StatefulWidget is used when the UI needs to change in response to things like user actions, timers, or API data.

Problem → Why → Solution

The problem — why this concept matters

If you change a state variable without calling setState(), the value may update, but the UI often won't re-render.

A common mistake

Assuming one screen's sample output proves every rebuild, overflow, accessibility, async, and lifecycle path.

Core concept — understand how it works

Treat Stateless vs Stateful as a contract for constraints, size, position, rebuild scope, semantics, and platform adaptation—not merely widget syntax. Prefer focused widgets, const constructors, lazy builders, and responsive constraints.

Mental model
  1. event
  2. state owner and notifier
  3. rebuilt widgets

Explain the widget, state, and lifecycle behavior of Stateless vs Stateful

Core syntax or pattern

dart
class GreetingText extends StatelessWidget {
  const GreetingText({super.key});

  @override
  Widget build(BuildContext context) {
    return const Text('Hello Flutter');
  }
}

class TapCounter extends StatefulWidget {
  const TapCounter({super.key});

  @override
  State<TapCounter> createState() => _TapCounterState();
}

class _TapCounterState extends State<TapCounter> {
  int taps = 0;

  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
      onPressed: () {
        setState(() {
          taps++;
        });
      },
      child: Text('Tapped $taps times'),
    );
  }
}

Treat Stateless vs Stateful as a contract for constraints, size, position, rebuild scope, semantics, and platform adaptation—not merely widget syntax. Prefer focused widgets, const constructors, lazy builders, and responsive constraints.

Read the code step by step

  1. eventclass GreetingText extends StatelessWidget {

    Put simply, Flutter widgets can be split into ones that don't change and ones that can. StatelessWidget is used for UI whose data/state never changes, while StatefulWidget is used when the UI needs to change in response to things like user actions, timers, or API data.

  2. state owner and notifierint taps = 0;

    Treat Stateless vs Stateful as a contract for constraints, size, position, rebuild scope, semantics, and platform adaptation—not merely widget syntax. Prefer focused widgets, const constructors, lazy builders, and responsive constraints.

  3. rebuilt widgets}

    Run and test an original Stateless vs Stateful example with small and large screens, text scaling, empty/loading/error states, rapid interaction, navigation restoration, and disposal cases. Use flutter analyze, widget tests, and profile mode where relevant.

Execution flow
  1. event
  2. state owner and notifier
  3. rebuilt widgets

Success

Explain the widget, state, and lifecycle behavior of Stateless vs Stateful

Failure

If you change a state variable without calling setState(), the value may update, but the UI often won't re-render.

Put simply, Flutter widgets can be split into ones that don't change and ones that can. StatelessWidget is used for UI whose data/state never changes, while StatefulWidget is used when the UI needs to change in response to things like user actions, timers, or API data.

dart
class GreetingText extends StatelessWidget {
  const GreetingText({super.key});

  @override
  Widget build(BuildContext context) {
    return const Text('Hello Flutter');
  }
}

class TapCounter extends StatefulWidget {
  const TapCounter({super.key});

  @override
  State<TapCounter> createState() => _TapCounterState();
}

class _TapCounterState extends State<TapCounter> {
  int taps = 0;

  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
      onPressed: () {
        setState(() {
          taps++;
        });
      },
      child: Text('Tapped $taps times'),
    );
  }
}
You should see
GreetingText shows unchanging text, and each time you tap the TapCounter button, the count goes up.

What's Next

In the Basic Widgets lesson, get hands-on with the UI widgets you'll use the most.

Real-world usage

How to apply it in a real project

Run and test an original Stateless vs Stateful example with small and large screens, text scaling, empty/loading/error states, rapid interaction, navigation restoration, and disposal cases. Use flutter analyze, widget tests, and profile mode where relevant.

Mini challenge

Build it yourself

Run and test an original Stateless vs Stateful example with small and large screens, text scaling, empty/loading/error states, rapid interaction, navigation restoration, and disposal cases. Use flutter analyze, widget tests, and profile mode where relevant.

  • Rebuild the example without copying it
  • Test a normal input and an edge case
  • Predict the compiler or output result before running it
dart
class GreetingText extends StatelessWidget {
  const GreetingText({super.key});

  @override
  Widget build(BuildContext context) {
    return const Text('Hello Flutter');
  }
}

class TapCounter extends StatefulWidget {
  const TapCounter({super.key});

  @override
  State<TapCounter> createState() => _TapCounterState();
}

class _TapCounterState extends State<TapCounter> {
  int taps = 0;

  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
      onPressed: () {
        setState(() {
          taps++;
        });
      },
      child: Text('Tapped $taps times'),
    );
  }
}

Expected: Explain the widget, state, and lifecycle behavior of Stateless vs Stateful

Show hint

Run and test an original Stateless vs Stateful example with small and large screens, text scaling, empty/loading/error states, rapid interaction, navigation restoration, and disposal cases. Use flutter analyze, widget tests, and profile mode where relevant.

Quick quiz 1 / 3

What best describes the core idea of Stateless vs Stateful?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

What to remember

  • event → state owner and notifier → rebuilt widgets
  • Treat Stateless vs Stateful as a contract for constraints, size, position, rebuild scope, semantics, and platform adaptation—not merely widget syntax. Prefer focused widgets, const constructors, lazy builders, and responsive constraints.
  • If you change a state variable without calling setState(), the value may update, but the UI often won't re-render.
  • Run and test an original Stateless vs Stateful example with small and large screens, text scaling, empty/loading/error states, rapid interaction, navigation restoration, and disposal cases. Use flutter analyze, widget tests, and profile mode where relevant.

Flutter documentation — Flutter

Easy traps

  • If you change a state variable without calling setState(), the value may update, but the UI often won't re-render.
  • Assuming one screen's sample output proves every rebuild, overflow, accessibility, async, and lifecycle path.
  • Doing expensive work in build(), leaking controllers or subscriptions, or calling setState over an unnecessarily large subtree.

Hands-on Exercise

Run and test an original Stateless vs Stateful example with small and large screens, text scaling, empty/loading/error states, rapid interaction, navigation restoration, and disposal cases. Use flutter analyze, widget tests, and profile mode where relevant.

You'll know it worked when: GreetingText shows unchanging text, and each time you tap the TapCounter button, the count goes up.

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Stateless vs Stateful | Thuta Learning