[Solved] Incorrect use of ParentDataWidget Error in Flutter
In this post, we are going to show you the cause and how to solve the "Incorrect use of ParentDataWidget" error in Flutter. This error occurs when the Child widget has not matched the parent widget.
══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═════════════════
The following assertion was thrown while applying parent data.:
Incorrect use of ParentDataWidget.
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.
The offending Expanded is currently placed inside a Padding widget.
The ownership chain for the RenderObject that received the incompatible parent data was:
RichText ← Text ← Expanded ← Padding ← Container ← _BodyBuilder ← MediaQuery ←
LayoutId-[<_ScaffoldSlot.body>] ← CustomMultiChildLayout ← AnimatedBuilder ← ⋯
In our case, this error happens due to the Expanded()
widget.
This error happens when child widgets like Flexible()
, Expanded()
, Positioned()
and TableCell()
widget has no matching parent widget. For example:
Container(
Expanded(
child: Text("Hello FlutterCampus")
)
) //Error: Incorrect use of ParentDataWidget
Here, Expanded()
widget must have Row()
, Column()
or Flex()
widget as Parent widget.
To solve this error, your child widget must have the expected parent widget. Some of the widget and their parent widgets are:
Widget | Parent Widget |
Expanded() | Row(), Column(), Flex() |
Flexible() | Row(), Column(), Flex() |
Positioned() | Stack() |
TableCell() | Table() |
For Example:
Row(
children:[
Expanded(
child: Text("Hello FlutterCampus")
)
]
)
OR:
Stack(
children:[
Positioned(
child: Text("Hello FlutterCampus")
)
]
)
Here, Row() is the parent widget of Expanded(), and Stack() is the parent of Positioned() widget. Similarly, see the above chart list, and use the parent according to the child widget.
In this way, you can solve the "Incorrect use of ParentDataWidget" Error in the Flutter App
Please Wait...
No any Comments on this Article