Expanded widgets must be placed inside Flex widgets

Crash

This crashes because Expanded is not a descendant of a Flex widget:

Container(
  child: Expanded(
    child: MyWidget(),
  ),
)

Acceptable

Here Expanded is a descendant of Flex:

Flex(
  direction: Axis.horizontal,
  children: [
    Expanded(
      child: MyWidget(),
    ),
  ],
)

Row is also a Flex widget:

Row(
  children: [
    Expanded(
      child: MyWidget(),
    ),
  ],
)

And so is Column:

Column(
  children: [
    Expanded(
      child: MyWidget(),
    ),
  ],
)

Another option is to just get rid of the Expanded widget:

Container(
  child: MyWidget(),
)

You do not have a Flex ancestor.

An Expanded widget must be a descendant of a Row, Column, or Flex, and the path from the Expanded widget to its enclosing Row, Column, or Flex must contain only StatelessWidgets or StatefulWidgets (not other kinds of widgets, like RenderObjectWidgets).

I am not sure about the need for Expanded in your case. But removing it or wrapping it in a Column should fix the problem.

Tags:

Dart

Flutter