Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

incorrect use of parent data widget. expanded widgets must be placed inside flex widgets

People also ask

What is incorrect use of ParentDataWidget?

This error occurs when the Child widget has not matched the parent widget.

How do you use Flex widget in flutter?

The Flex widget allows you to control the axis along which the children are placed (horizontal or vertical). This is referred to as the main axis. If you know the main axis in advance, then consider using a Row (if it's horizontal) or Column (if it's vertical) instead, because that will be less verbose.

What is ParentDataWidget?

ParentDataWidget<T extends ParentData> class Null safety. Base class for widgets that hook ParentData information to children of RenderObjectWidgets. This can be used to provide per-child configuration for RenderObjectWidgets with more than one child.


You should use Expanded only within a column, row or flex.


In this case just removing Expanded or wrapping it in a Column should fix the problem.

An Expanded widget must be a descendant of a Row, Column, or Flex

The fix for this above issue, is to move the expanded widget inside a flex widget. Example below.

Row(
    children: [
      Expanded(
        child: Column(
          children: [
            Container(
              padding: EdgeInsets.only(bottom: 8.0),
              child: Text(
                "Some Lage Somthing",
                style: TextStyle(fontWeight: FontWeight.bold),
              ),
            ),
            Text("Someplace, Country")
          ],
        ),
      ),
      Icon(Icons.star)
    ],
  ),
);

Happy Fluttering!


First Rule: use Expanded only within a column, row or flex.

Second Rule: Parent column that have expanded child column must be wrapped with expanded as well


there are may things to get this problem

  1. Under ListView don't use Spacer Widget
  2. don't use Positiond under Row or Column
  3. Expanded can only use it must be a descendant of column row Flex

there's no expanded in my code so i found that using Spacer() too can cause this error if it's inside a listview


In my case, I was using Positioned inside a column. Removing it solved the problem!