0Day Forums
Expanded widgets must be placed inside Flex widgets - Printable Version

+- 0Day Forums (https://zeroday.vip)
+-- Forum: Coding (https://zeroday.vip/Forum-Coding)
+--- Forum: Flutter & Dart (https://zeroday.vip/Forum-Flutter-Dart)
+--- Thread: Expanded widgets must be placed inside Flex widgets (/Thread-Expanded-widgets-must-be-placed-inside-Flex-widgets)



Expanded widgets must be placed inside Flex widgets - ayazw - 07-21-2023

New to flutter, can some one tells me whats wrong with below code[![enter image description here][1]][1]

class GamePage extends StatelessWidget {
int _row;
int _column;

GamePage(this._row,this._column);

@override
Widget build(BuildContext context) {
return new Material(
color: Colors.deepPurpleAccent,
child:new Expanded(
child:new GridView.count(crossAxisCount: _column,children: new List.generate(_row*_column, (index) {
return new Center(
child: new CellWidget()
);
}),) )


);
}
}

Attaching error screenshot.


[1]:



RE: Expanded widgets must be placed inside Flex widgets - determinantal756547 - 07-21-2023

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.


RE: Expanded widgets must be placed inside Flex widgets - renae691 - 07-21-2023

# 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(),
)



RE: Expanded widgets must be placed inside Flex widgets - kaydencelwcjlrqj - 07-21-2023

Another simple example related to the error but with Expanded and ConstrainedBox :

The code:

Column(
children: [
ConstrainedBox(
child: Expanded(
child: ...
)
)
]

The error message:
> [...] 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 ConstrainedBox widget.

Why the code is the code not working properly ?
In this example, Expanded must have the Column as direct Parent (which is a compatible parent type).

The solution:

Column(
children: [
Expanded(
child: ConstrainedBox(
child: ...
)
)
]









RE: Expanded widgets must be placed inside Flex widgets - scree38 - 07-21-2023

You need to try using `Flex` Widget on that


RE: Expanded widgets must be placed inside Flex widgets - foxworth954 - 07-21-2023

sometimes it can also happen if you have an expended widget inside the SingleChildScrollView widget, then it shows the blank screen. If you have any expended widget inside SingleChildScrollView remove it and it will solve the problem.

**Before**

body: SingleChildScrollView(
child: Column(
children: [
Container(),
Expanded(
child: Container(),
),
],
),
),

**After**

body: SingleChildScrollView(
child: Column(
children: [
Container(),
Container(),
],
),
),




RE: Expanded widgets must be placed inside Flex widgets - noreanwbcjuhowy - 07-21-2023

In my case issue was solved



Flex(
direction: Axis.vertical,
children: [
Expanded(
child: Text("Issue was solved"),
)
])