展开的窗口小部件引发“ RenderBox未被布置”异常

时间:2019-04-08 11:51:50

标签: flutter

我是新手。我正在设置一个包含ListView Widget的Expanded Widget。扩展小部件位于列小部件中。但是运行代码会导致“ RenderBox未被布局”异常。如何修复代码?

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

错误:

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

2 个答案:

答案 0 :(得分:0)

Use Flexible instead of Expanded when using it with ListView or ScrollView.

答案 1 :(得分:0)

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