Are you struggling with the “Incorrect use of ParentDataWidget” error in Flutter? Read this article to find out how to solve it and get your app up and running smoothly.
The ParentDataWidget Expanded(flex: 1) wants to apply ParentData of type FlexParentData to a RenderObject, which has been set up to accept ParentData of incompatible type BoxParentData.
Usually, this means that the Expanded widget has the wrong ancestor RenderObjectWidget. Typically, Expanded widgets are placed directly inside Flex widgets.
Table of Contents
Steps to Fix Incorrect use of ParentDataWidget in Flutter
The offending Expanded is currently placed inside a Center widget.
If your terminal complain the Incorrect use of ParentDataWidget. I sure you broke the framework rules by using child widget to used as parent widget.
Check your code probably you wrap Expanded or Flexible outside of Column or Row.
Error
Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
Container(
// color: Colors.grey,
decoration: const BoxDecoration(color: Colors.grey),
child: Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
),
],
),
),
Related Reading: [Solved] Incorrect use of ParentDataWidget by using positioned widget
Solution
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Expanded(
child: const Text(
'You have pushed the button this many times:',
),
),
Container(
// color: Colors.grey,
decoration: const BoxDecoration(color: Colors.grey),
child: Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
),
],
),
Follow framework guidelines and read terminal message clearly once.