简体   繁体   中英

Expanded Widget throws “ RenderBox was not laid out” Exception

I'm new to flutter. I'm setting up an Expanded Widget which contains a ListView Widget. the Expanded Widget sits in a Column Widget. But running the code gives "RenderBox was not laid out" Exception. How do I fix the code?

 Widget build(BuildContext context) {
    return ListView(
      children: products
          .map(
            (element) => Card(
                  child: Column(
                    children: <Widget>[
                      Text(element)
                    ],
                  ),
                ),
          )
          .toList(),
    );
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Container(
          margin: EdgeInsets.all(10.0),
          child:   ProductControl(_addProduct),
        ),
        Expanded(
          child: Products(_products),
        )
      ],
    );
  }

error:

I/flutter ( 9914): Another exception was thrown: RenderBox was not laid out: RenderFlex#e84ab relayoutBoundary=up2 NEEDS-PAINT

ListViewScrollView一起使用时,使用Flexible而不是Expanded

You can use Flexible or Try wrapping expanded in a container with fixed width and heigh

    Widget build(BuildContext context) {
        return Column(
          children: <Widget>[
            Container(
              margin: EdgeInsets.all(10.0),
              child:   ProductControl(_addProduct),
            ),
           container(
            width:300,//set as required
            height:300,
            child:  Expanded(
              child: Products(_products),
            ),
           ),
          ],
        );
      }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM