为什么我会收到错误:RenderBox 未布局:RenderViewport#a3518 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE

ale*_*exc 7 flutter

我不断收到此错误:“ RenderBox 未布局:RenderViewport#a3518 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE 'package:flutter/src/rendering/box.dart': 断言失败:第 1785 行 pos 12 : 'hasSize' " 当我运行此代码时:

(我需要的是在“FutureBuilder”之前有“Container”,在“FutureBuilder”之后有“RaishedButton”,因为“FutureBuilder”可以运行多次,而我只需要一个容器和一个RaishedButton)。

请帮帮我!

@override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: new AppBar(
        title: new Text(
          'OrderLunch',
          style: TextStyle(
            fontSize: 26.0,
            fontWeight: FontWeight.bold,
          ),
        ),
        centerTitle: true,
      ),
      body: Container(
        height: 595,
        width: 800,///not accurate
        decoration: BoxDecoration(
          image: DecorationImage(
            image: AssetImage('assets/portocale.jpg'),
          ),
          borderRadius: BorderRadius.all(Radius.circular(8.0)),
        ),
        child: Column(
          children: [
            Container(
              decoration: BoxDecoration(
                border: Border.all(
                  width: 3.5,
                  color: Colors.blue[400],
                ),
                borderRadius: BorderRadius.all(
                  Radius.circular(20),
                ),
                color: Colors.white,
              ),
              //padding: EdgeInsets.all(8),
              //color: Colors.white,
              child: Text(
                'copil/copii:',
                style: TextStyle(
                  color: Colors.black.withOpacity(1),
                  fontWeight: FontWeight.bold,
                  fontSize: 23.0,
                ),
              ),
            ),
            FutureBuilder(
              future: getMethod(),
              builder: (BuildContext context, AsyncSnapshot snapshot) {
                List snap =  snapshot.data;

                if (snapshot.connectionState == ConnectionState.waiting) {
                  return Center(
                    child: CircularProgressIndicator(),
                  );
                }
                if (snapshot.hasError) {
                  return Center(
                    child: Text('Error fatching Data'),
                  );
                }
                return ListView.builder(
                  itemCount: snap.length,
                  itemBuilder: (context, index) {
                    return ListTile(
                      title: Column(
                        children: [
                          SizedBox(height: 50.0),
                          Row(
                            children: [
                              Text('         '),
                              Container(
                                decoration: BoxDecoration(
                                  border: Border.all(
                                    width: 4,
                                    color: Colors.white,
                                  ),
                                  borderRadius: BorderRadius.all(
                                    Radius.circular(4),
                                  ),
                                  color: Colors.white,
                                ),
                                child: Text(
                                  "${snap[index]['elevNume']} ${snap[index]['elevPrenume']}:",
                                  style: TextStyle(
                                    decoration: TextDecoration.underline,
                                    color: Colors.black,
                                    fontSize: 22.0,
                                  ),
                                ),
                              ),
                            ],
                          ),
                          Row(
                            children: [
                              Text('         '),
                              Container(
                                decoration: BoxDecoration(
                                  border: Border.all(
                                    width: 4,
                                    color: Colors.white,
                                  ),
                                  borderRadius: BorderRadius.all(
                                    Radius.circular(4),
                                  ),
                                  color: Colors.white,
                                ),
                                child: Text(
                                  'Ii se va comanda maine mancare:',
                                  style: TextStyle(
                                    color: Colors.black,
                                    fontSize: 17.0,
                                  ),
                                ),
                              ),
                              Text('  '),
                              _buildFirstDate(),
                            ],
                          ),
                          SizedBox(height: 300.0),
                        ],
                      ),
                    );
                  },
                );
              },
            ),
            RaisedButton(
              color: Colors.blue[400],
              child: Text(
                'Save changes',
                style: TextStyle(
                  fontSize: 19.0,
                ),
              ),
              onPressed: (){
                updateMethod();
                if(b==1){
                  Text(
                    'Changes have been successfully saved!!',
                    style: TextStyle(
                      fontSize: 20.0,
                    ),
                  );
                  print(':)');
                  b=0;
                }
              },
            ),
          ],
        ),
      ),
    );
  }
}

Run Code Online (Sandbox Code Playgroud)

但是当我运行这个时,我没有收到任何错误:

(但就像我之前说过的,这不是我需要的,因为“FutureBuilder”可以运行多次,但我不希望我的“容器”和“RaishedButton”显示多次)

@override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: new AppBar(
        title: new Text(
          'OrderLunch',
          style: TextStyle(
            fontSize: 26.0,
            fontWeight: FontWeight.bold,
          ),
        ),
        centerTitle: true,
      ),
      body: Container(
        height: 595,
        width: 800,///not accurate
        decoration: BoxDecoration(
          image: DecorationImage(
            image: AssetImage('assets/portocale.jpg'),
          ),
          borderRadius: BorderRadius.all(Radius.circular(8.0)),
        ),
        child: FutureBuilder(
          future: getMethod(),
          builder: (BuildContext context, AsyncSnapshot snapshot) {
            List snap =  snapshot.data;

            if (snapshot.connectionState == ConnectionState.waiting) {
              return Center(
                child: CircularProgressIndicator(),
              );
            }
            if (snapshot.hasError) {
              return Center(
                child: Text('Error fatching Data'),
              );
            }
            return ListView.builder(
              itemCount: snap.length,
              itemBuilder: (context, index) {
                return ListTile(
                  title: Column(
                    children: [
                      Container(
                        decoration: BoxDecoration(
                          border: Border.all(
                            width: 3.5,
                            color: Colors.blue[400],
                          ),
                          borderRadius: BorderRadius.all(
                            Radius.circular(20),
                          ),
                          color: Colors.white,
                        ),
                        //padding: EdgeInsets.all(8),
                        //color: Colors.white,
                        child: Text(
                          'copil/copii:',
                          style: TextStyle(
                            color: Colors.black.withOpacity(1),
                            fontWeight: FontWeight.bold,
                            fontSize: 23.0,
                          ),
                        ),
                      ),
                      SizedBox(height: 50.0),
                      Row(
                        children: [
                          Text('         '),
                          Container(
                            decoration: BoxDecoration(
                              border: Border.all(
                                width: 4,
                                color: Colors.white,
                              ),
                              borderRadius: BorderRadius.all(
                                Radius.circular(4),
                              ),
                              color: Colors.white,
                            ),
                            child: Text(
                              "${snap[index]['elevNume']} ${snap[index]['elevPrenume']}:",
                              style: TextStyle(
                                decoration: TextDecoration.underline,
                                color: Colors.black,
                                fontSize: 22.0,
                              ),
                            ),
                          ),
                        ],
                      ),
                      Row(
                        children: [
                          Text('         '),
                          Container(
                            decoration: BoxDecoration(
                              border: Border.all(
                                width: 4,
                                color: Colors.white,
                              ),
                              borderRadius: BorderRadius.all(
                                Radius.circular(4),
                              ),
                              color: Colors.white,
                            ),
                            child: Text(
                              'Ii se va comanda maine mancare:',
                              style: TextStyle(
                                color: Colors.black,
                                fontSize: 17.0,
                              ),
                            ),
                          ),
                          Text('  '),
                          _buildFirstDate(),
                        ],
                      ),
                      SizedBox(height: 300.0),
                      RaisedButton(
                        color: Colors.blue[400],
                        child: Text(
                          'Save changes',
                          style: TextStyle(
                            fontSize: 19.0,
                          ),
                        ),
                        onPressed: (){
                          updateMethod();
                          if(b==1){
                            Text(
                              'Changes have been successfully saved!!',
                              style: TextStyle(
                                fontSize: 20.0,
                              ),
                            );
                            print(':)');
                            b=0;
                          }
                        },
                      ),
                    ],
                  ),
                );
              },
            );
          },
        ),
      ),
    );
  }
}

Run Code Online (Sandbox Code Playgroud)

And*_*say 14

您正在尝试ListView.builder在 a 中使用,FutureBuilder而 a 中没有大小Column,因此您需要以某种方式使用SizedBox(height: 300, child: ListView.builder())或使用Expanded小部件来约束它 \xe2\x80\x93

\n

查看这篇文章https://flutter.dev/docs/development/ui/layout/constraints

\n


Ras*_*ran 6

考虑使用“shrinkWrap”属性(或 ShrinkWrappingViewport)将视口的高度调整为其子级高度的总和。因此,在 ListView.builder 小部件中,将ShrinkWrap 属性添加为 true。

ListView.builder(
  
   shrinkWrap: true, ///////////////////////Use This Line 

 )
Run Code Online (Sandbox Code Playgroud)