children have non-zero flex but incoming height constraints are unbounded

Viewed 11242
return Card(
  child: Row(
    children: <Widget>[
      Placeholder(
        fallbackHeight: 100,
        fallbackWidth: 100,
      ),
      Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          Container(height: 10,  child: Container(child: Text("One"),)),
          Expanded(child: Container(child:  Text("Center") )),

        ],
      )
    ],
  ),
);

On the code above I am getting error:

I/flutter ( 4872): The following assertion was thrown during performLayout(): I/flutter ( 4872): RenderFlex children have non-zero flex but incoming height constraints are unbounded. I/flutter ( 4872): When a column is in a parent that does not provide a finite height constraint, for example if it is I/flutter ( 4872): in a vertical scrollable, it will try to shrink-wrap its children along the vertical axis. Setting a I/flutter ( 4872): flex on a child (e.g. using Expanded) indicates that the child is to expand to fill the remaining I/flutter ( 4872): space in the vertical direction.

Whole code:

class FilmItems extends StatelessWidget {
  List<String> filmListList;

  List<String> _getFilmList() {
    var items = List<String>.generate(101, (counter) => "item $counter");
    return items;
  }

  FilmItems() {
    filmListList = _getFilmList();
  }

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
        itemCount: filmListList.length,
        itemBuilder: (BuildContext context, int index) {
          return Card(
            child: Row(
              children: <Widget>[
                Placeholder(
                  fallbackHeight: 100,
                  fallbackWidth: 100,
                ),
                Column(
                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[
                    Container(height: 10,  child: Container(child: Text("One"),)),
                    Expanded(child: Container(child:  Text("Center") )),

                  ],
                )
              ],
            ),
          );
        });
  }
}

What is wrong?

3 Answers

The problem is that you are using Expanded and any of it's parents have an explicit height.

The solution would depend on how do you want to handle the height of the Expanded. In your case, seems to be that you want to have a Row with a fixed height equal to the Placeholder. In that case, you need to wrap the Row with the same height as the Placeholder, like this:

@override
Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(title: Text("")),
    body: ListView.builder(
        itemCount: 3,
        itemBuilder: (BuildContext context, int index) {
          return Card(
            child: SizedBox(
              height: 100,
              child: Row(
                children: <Widget>[
                  Placeholder(
                    fallbackHeight: 100,
                    fallbackWidth: 100,
                  ),
                  Column(
                    mainAxisSize: MainAxisSize.min,
                    children: <Widget>[
                      Text("One"),
                      Expanded(child: Center(child: Text("Center"))),
                    ],
                  ),
                ],
              ),
            ),
          );
        }),
  );
}

I removed the height 10 of the Text("One") because if the fontSize is bigger, the text would look cropped. And I wrapped the Text("Center") with a Center widget, I think that's what you wanted to achieve.

Suggestion: If the content inside the Row could haven a bigger height than the Row, the content would look cropped. If that could happen you might want to take another approach.

When you are using Column widget, it's parent should have a finite height. So in your code the parent is a row and it also does not have a finite height. You need wrap Column with Container and give a finite height.

Container(
     height: 500,
     child: Column()
)

Put your Column/Row inside an Expanded or SizedBox (with some height) like this:

Expanded(
  child: Column(...)
)

Or

SizedBox(
  height: 250, // give some height
  child: Column(...),
)

Related