Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

Scaffold rejects my custom app bar widget as the wrong type

ErrorThe AppBarDesign can't be assigned to the parameter type 'PreferredSizeWidget'

Short answer

Scaffold.appBar needs an object that is a PreferredSizeWidget. A plain StatelessWidget or Container is not one, so Dart refuses it. Either make your widget implement PreferredSizeWidget and add a preferredSize getter, or wrap the widget in PreferredSize and give it a height.

  • Applies to Flutter 3.3x stable, Dart 3.x
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Open the file that holds your app bar class and look at its declaration line. If it says only extends StatelessWidget or extends StatefulWidget, the type has no way to report a size, and cause 1 applies. If the value you pass is a built-in widget such as Container or SizedBox, go to cause 2.

    grep -rn "appBar:" lib/
  2. 2Match what you see, then jump to its fix

    1. 1The type in the message is your own class name, for example AppBarDesign.Your widget class does not implement PreferredSizeWidgetFix
    2. 2The type in the message is Container, SizedBox, Row or another built-in widget.You pass an ordinary widget where the parent wants a preferred sizeFix

What does the error look like?

  • The AppBarDesign can't be assigned to the parameter type 'PreferredSizeWidget'

Cause 1 of 2

Your widget class does not implement PreferredSizeWidget

1Check

The Flutter API says implementers of PreferredSizeWidget must provide a preferredSize getter that returns a Size. A class declared only as a widget does not have it.

grep -n "class .* extends" lib/widgets/app_bar_design.dart

2Fix

Add implements PreferredSizeWidget to the class and override preferredSize. Return the height you want the bar to have.

class AppBarDesign extends StatelessWidget implements PreferredSizeWidget {
  const AppBarDesign({super.key});

  @override
  Size get preferredSize => const Size.fromHeight(56);

  @override
  Widget build(BuildContext context) {
    return AppBar(title: const Text('Home'));
  }
}

3Verify

Pass the widget to Scaffold. The analyzer error disappears.

Scaffold(appBar: const AppBarDesign(), body: const SizedBox())
// then
flutter analyze

Confirmed by api.flutter.dev

Did this fix it?

Cause 2 of 2

You pass an ordinary widget where the parent wants a preferred size

1Check

Scaffold asks its app bar to report a preferred size. Widgets such as Container do not implement PreferredSizeWidget, while AppBar does.

grep -n -A3 "appBar:" lib/main.dart

2Fix

Wrap the widget in PreferredSize. It takes a required preferredSize and a required child, and it does not add constraints to the child.

Scaffold(
  appBar: PreferredSize(
    preferredSize: const Size.fromHeight(80),
    child: Container(
      color: Colors.indigo,
      alignment: Alignment.center,
      child: const Text('Home'),
    ),
  ),
  body: const SizedBox(),
)

3Verify

Run the analyzer, then hot reload. The bar should be 80 logical pixels tall.

flutter analyze

Confirmed by api.flutter.dev

Did this fix it?

How do I stop it coming back?

  • When you build a reusable app bar, declare it with implements PreferredSizeWidget from the start.
  • Return the same height from preferredSize that your layout uses.
  • Run flutter analyze before commit so type errors show up in the terminal, not at build time.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.