setState()或markNeedsBuild在构建期间被调用

17 浏览
0 Comments

setState()或markNeedsBuild在构建期间被调用

class MyHome extends StatefulWidget {
  @override
  State createState() => new MyHomePage2();
}
class MyHomePage2 extends State {
  List items = new List();
  buildlist(String s) {
    setState(() {
      print("entered buildlist" + s);
      List refresh = new List();
      if (s == 'button0') {
        refresh = [
          new Refreshments("Watermelon", 250),
          new Refreshments("Orange", 275),
          new Refreshments("Pine", 300),
          new Refreshments("Papaya", 225),
          new Refreshments("Apple", 250),
        ];
      } else if (s == 'button1') {
        refresh = [
          new Refreshments("Pina Colada", 250),
          new Refreshments("Bloody Mary", 275),
          new Refreshments("Long Island Ice tea", 300),
          new Refreshments("Screwdriver", 225),
          new Refreshments("Fusion Cocktail", 250),
        ];
      } else if (s == 'button2') {
        refresh = [
          new Refreshments("Virgin Pina Colada", 250),
          new Refreshments("Virgin Mary", 275),
          new Refreshments("Strawberry Flush", 300),
          new Refreshments("Mango Diver", 225),
          new Refreshments("Peach Delight", 250),
        ];
      } else {
        refresh = [
          new Refreshments("Absolute", 250),
          new Refreshments("Smirnoff", 275),
          new Refreshments("White Mischief", 300),
          new Refreshments("Romanov", 225),
          new Refreshments("Blender's Pride", 250),
        ];
      }
      for (var item in refresh) {
        items.add(new ItemsList(item));
      }
    });
  }
  @override
  Widget build(BuildContext context) {
    var abc = MediaQuery.of(context).size;
    print(abc.width);
    var width = abc.width / 4;
    Text text = new Text("Dev");
    Text text2 = new Text("Sneha");
    Text text3 = new Text("Prashant");
    Text text4 = new Text("Vikesh");
    var pad = const EdgeInsets.all(10.0);
    Padding pad1 = new Padding(child: text, padding: pad);
    Padding pad2 = new Padding(child: text2, padding: pad);
    Padding pad3 = new Padding(child: text3, padding: pad);
    Padding pad4 = new Padding(child: text4, padding: pad);
    ListView listView = new ListView(children: [
      new Image.asset('images/party.jpg'),
      pad1,
      pad2,
      pad3,
      pad4
    ]);
    Drawer drawer = new Drawer(child: listView);
    return new Scaffold(
      drawer: drawer,
      appBar: new AppBar(
        title: new Text('Booze Up'),
      ),
      body: new Column(children: [
        new ListView.builder(
          scrollDirection: Axis.horizontal,
          itemCount: 4,
          itemBuilder: (BuildContext context, int index) {
            return new Column(children: [
              new Container(
                child: new Flexible(
                    child: new FlatButton(
                  child: new Image.asset('images/party.jpg',
                      width: width, height: width),
                  onPressed: buildlist('button' + index.toString()),
                )),
                width: width,
                height: width,
              )
            ]);
          },
        ),
        new Expanded(
            child: new ListView(
          padding: new EdgeInsets.fromLTRB(10.0, 10.0, 0.0, 10.0),
          children: items,
          scrollDirection: Axis.vertical,
        )),
      ]),
      floatingActionButton: new FloatingActionButton(
        onPressed: null,
        child: new Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}
class Refreshments {
  String name;
  int price;
  Refreshments(this.name, this.price);
}
class ItemsList extends StatelessWidget {
  final Refreshments refreshments;
  ItemsList(this.refreshments);
  @override
  Widget build(BuildContext context) {
    return new ListTile(
      onTap: null,
      title: new Text(refreshments.name),
    );
  }
}

完整代码

我有两个错误:

1] 水平视口给出了无限高度。

在给定的垂直空间中,水平视口被提供了无限的扩展空间。

2] 在 build 中调用了 setState() 或 markNeedsBuild

一个垂直渲染溢出了99488像素。

请帮我解决它。我正在创建这个应用程序,在每个图像单击时,下面应该显示一个列表。图像将在一行中,列表应显示在行下面。

谢谢。

admin 更改状态以发布 2023年5月21日
0
0 Comments

您的代码

onPressed: buildlist('button'+index.toString()),

执行了 buildlist() 并将结果传递给 onPressed,但这不是期望的行为。

应该是这样的

onPressed: () => buildlist('button'+index.toString()),

通过这种方式,将函数(闭包)传递给 onPressed,当执行时,调用 buildlist()

0
0 Comments

在我的情况下,我在build方法完成构建小部件的过程之前调用了setState方法。

如果在build方法完成之前显示snackBaralertDialog,以及许多其他情况,可能会遇到此错误。因此,在这种情况下,您应该使用以下回调函数:

WidgetsBinding.instance.addPostFrameCallback((_){
  // Add Your Code here.
});

或者您也可以使用完成相同操作的SchedulerBinding

SchedulerBinding.instance.addPostFrameCallback((_) {
  // add your code here.
  Navigator.push(
        context,
        new MaterialPageRoute(
            builder: (context) => NextPage()));
});

0