Scaffold rejects my custom app bar widget as the wrong type
The 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.
Fix it fast
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/2Match what you see, then jump to its fix
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.dart2Fix
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 analyzeConfirmed by api.flutter.dev
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.dart2Fix
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 analyzeConfirmed by api.flutter.dev
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- Qist BazaarA buy-now-pay-later marketplace built from a blank repo, now past 100K downloads on Google Play.
- SessionBirdAn omnichannel engagement suite in Flutter: campaigns, chatbot, tickets, analytics and live calling.
Related fixes
Mobile apps
AudioCache has no play method after updating audioplayers
The method 'play' isn't defined for the type 'AudioCache'.
Since audioplayers 1.0, AudioCache only turns asset files into local files and caches them.
2 causes, each with a fixMobile apps
Flutter Bloc throws that emit was called after the event handler completed
emit was called after an event handler completed normally.
Each on<Event> handler gets its own Emitter, and it stops working the moment the handler's returned Future completes.
2 causes, each with a fixMobile apps
analysis_options.yaml: the URI it includes can't be found
include_file_not_found
The analyzer reports include_file_not_found when the file named after include: in analysis_options.yaml does not resolve.
2 causes, each with a fixMobile apps
Dart error: the body might complete normally and return null for a non-nullable type
body_might_complete_normally
Dart found a path through your function or callback that reaches the closing brace without a return.
3 causes, each with a fixMobile apps
Dart throws ConcurrentModificationError while looping over a list
ConcurrentModificationError
Dart throws this when a collection changes size while something iterates it.
2 causes, each with a fixMobile apps
Dart cannot turn a compact timestamp like 20201101153000 into a DateTime
FormatException: Invalid date format
DateTime.parse accepts a subset of ISO 8601, and a time part must be introduced by T or a space.
2 causes, each with a fix