简体   繁体   English

扩展的小部件抛出“RenderBox没有布局”异常

[英]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. 我正在设置一个包含ListView Widget的Expanded Widget。 the Expanded Widget sits in a Column Widget. Expanded Widget位于Column Widget中。 But running the code gives "RenderBox was not laid out" Exception. 但是运行代码会给出“RenderBox没有布局”的例外情况。 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 您可以在固定宽度和高度的容器中使用Flexible或Try包装

    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),
            ),
           ),
          ],
        );
      }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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