【问题标题】:List builder versus for loops列表生成器与 for 循环
【发布时间】:2020-05-19 00:06:49
【问题描述】:

我有一段代码——有效!所以,不要寻求调试帮助。

我已经学习了三天,而且大部分时间都是积极的,但有时很难找到我正在尝试做的事情的工作代码示例。所以,大量的试验和错误。我最好的建议是将每个小部件的背景颜色设置为粗体(黄色、绿色、蓝色),以帮助解决设计/布局问题。

但是,我读了很多关于如何实现这样的东西:从 API 检索 json 数据,遍历它以使用 Future 创建一个动态的小部件列表。我这个时候的例子是纯flutter web。

问题是 - 许多网站也建议使用 List Builder,而在我的一生中,我无法让它工作。它一直溢出浏览器的底部,我找不到任何扩展它的方法。我使用了“for Model in List”循环,效果很好。

所以,我的问题是 - 如果我给你工作代码,你能提出更好的方法/实现吗?

 Widget build(BuildContext context) {
    return FutureBuilder<List<Menurender>>(
        future: fetchMenu(),
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.none &&
              snapshot.hasData == null) {
            //print('project snapshot data is: ${projectSnap.data}');
            return Container();
          }
          return Container(
            width: MediaQuery.of(context).size.width,
            //color: Colors.yellow,
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Container(
                  width: MediaQuery.of(context).size.width * .80,
                  child: SingleChildScrollView(
                      child: Column(children: [
                    for (Menurender menu in snapshot.data)
                      Column(children: <Widget>[
                        Row(
                          children: <Widget>[
                            //SizedBox(width: 60),
                            Text(
                              '${menu.weekDayFull}',
                              style: TextStyle(
                                  color: Color(0xff444444),
                                  fontSize: 32,
                                  letterSpacing: 2,
                                  fontWeight: FontWeight.w600,
                                  fontFamily: 'Fira Sans'),
                            ),
                          ],
                        ),
                        Row(
                          children: <Widget>[
                            //SizedBox(width: 60),
                            Column(
                              children: <Widget>[
                                Text(
                                  '${menu.dayOfMonth} ${menu.monthFull}',
                                  style: TextStyle(
                                      color: Color(0xff333333),
                                      fontSize: 14,
                                      fontWeight: FontWeight.w400,
                                      fontFamily: 'Fira Sans'),
                                ),
                              ],
                            ),
                            Expanded(
                                child: Column(
                              children: <Widget>[
                                Padding(
                                  padding:
                                      const EdgeInsets.fromLTRB(10.0, 0, 0, 0),
                                  child: Container(
                                    decoration: BoxDecoration(
                                        //color: Colors.red,
                                        border: Border(
                                      bottom: BorderSide(
                                          color: Color(0xff4A4A4A4A)),
                                    )),
                                  ),
                                ),
                              ],
                            ))
                          ],
                        )
                      ])
                  ])),
                ),
              ],
            ),
          );
        });
  }

实际工作的代码在这里,因此您可以看到我要完成的工作: 我的测试正在进行中: https://flavorboxstorage.z23.web.core.windows.net/#/

实际的公司 wordpress 网站: https://www.flavorboxsaigon.com/

【问题讨论】:

  • 我的回答对你有用吗?
  • 我仍在努力找出问题所在。
  • 我遇到了错误
  • 抛出了另一个异常:A RenderFlex 在右侧溢出了 98464 像素。引发了另一个异常:垂直视口被赋予了无限的高度。抛出了另一个异常:断言失败:org-dartlang-app:///packages/flutter/src/rendering/box.dart:1694:12 抛出了另一个异常:断言失败:org-dartlang-app:///packages /flutter/src/rendering/box.dart:1694:12 另一个异常抛出:断言失败:org-dartlang-app:///packages/flutter/src/rendering/box.dart:1694:12
  • 我想出了无限的高度。 renderflex 溢出也是我的 for 循环中的一个错误。所以现在解决断言失败错误

标签: flutter dart flutter-web


【解决方案1】:

在小部件树中编写 for 循环会使 Flutter 在重建小部件时变得困难,并可能影响性能。您应该可以像这样使用 ListView.builder:

ListView.builder(
  itemCount: snapshot.data.length,
  itemBuilder: (context, index) {
    return Column(
      children: <Widget>[
        Row(
          children: <Widget>[
            //SizedBox(width: 60),
            Text(
              '${snapshot.data[index].weekDayFull}',
              style: TextStyle(
                color: Color(0xff444444),
                fontSize: 32,
                letterSpacing: 2,
                fontWeight: FontWeight.w600,
                fontFamily: 'Fira Sans'
              ),
            ),
          ],
        ),
        Row(
          children: <Widget>[
            //SizedBox(width: 60),
            Column(
              children: <Widget>[
                Text(
                  '${snapshot.data[index].dayOfMonth} ${snapshot.data[index].monthFull}',
                  style: TextStyle(
                    color: Color(0xff333333),
                    fontSize: 14,
                    fontWeight: FontWeight.w400,
                    fontFamily: 'Fira Sans'
                  ),
                ),
              ],
            ),
            Expanded(
              child: Column(
                children: <Widget>[
                  Padding(
                    padding: const EdgeInsets.fromLTRB(10.0, 0, 0, 0),
                    child: Container(
                      decoration: BoxDecoration(
                        //color: Colors.red,
                        border: Border(
                          bottom: BorderSide(
                            color: Color(0xff4A4A4A4A)
                          ),
                        )
                      ),
                    ),
                  ),
                ],
              )
            )
          ],
        )
      ]
    );
  }
)                          

【讨论】:

    【解决方案2】:

    如果你想使用 for 循环,你可以这样做

    ListView(
       children: <Widget>[
         for(var x=0; x<snapshot.data.length;x++)
         Container(
            child: Column(children: <Widget>[
                        Row(
                          children: <Widget>[
                            //SizedBox(width: 60),
                            Text(
                              '${menu.weekDayFull}',
                              style: TextStyle(
                                  color: Color(0xff444444),
                                  fontSize: 32,
                                  letterSpacing: 2,
                                  fontWeight: FontWeight.w600,
                                  fontFamily: 'Fira Sans'),
                            ),
                          ],
                        ),
                        Row(
                          children: <Widget>[
                            //SizedBox(width: 60),
                            Column(
                              children: <Widget>[
                                Text(
                                  '${menu.dayOfMonth} ${menu.monthFull}',
                                  style: TextStyle(
                                      color: Color(0xff333333),
                                      fontSize: 14,
                                      fontWeight: FontWeight.w400,
                                      fontFamily: 'Fira Sans'),
                                ),
                              ],
                            ),
                            Expanded(
                                child: Column(
                              children: <Widget>[
                                Padding(
                                  padding:
                                      const EdgeInsets.fromLTRB(10.0, 0, 0, 0),
                                  child: Container(
                                    decoration: BoxDecoration(
                                        //color: Colors.red,
                                        border: Border(
                                      bottom: BorderSide(
                                          color: Color(0xff4A4A4A4A)),
                                    )),
                                  ),
                                ),
                              ],
                            ))
         )
       ]
    )
    

    如果你想要 ListView.builder

    ListView.builder(
       itemCount: snapshot.data.length,
       itemBuilder: (context, index){
           return Container(
            child: Column(children: <Widget>[
                        Row(
                          children: <Widget>[
                            //SizedBox(width: 60),
                            Text(
                              '${menu.weekDayFull}',
                              style: TextStyle(
                                  color: Color(0xff444444),
                                  fontSize: 32,
                                  letterSpacing: 2,
                                  fontWeight: FontWeight.w600,
                                  fontFamily: 'Fira Sans'),
                            ),
                          ],
                        ),
                        Row(
                          children: <Widget>[
                            //SizedBox(width: 60),
                            Column(
                              children: <Widget>[
                                Text(
                                  '${menu.dayOfMonth} ${menu.monthFull}',
                                  style: TextStyle(
                                      color: Color(0xff333333),
                                      fontSize: 14,
                                      fontWeight: FontWeight.w400,
                                      fontFamily: 'Fira Sans'),
                                ),
                              ],
                            ),
                            Expanded(
                                child: Column(
                              children: <Widget>[
                                Padding(
                                  padding:
                                      const EdgeInsets.fromLTRB(10.0, 0, 0, 0),
                                  child: Container(
                                    decoration: BoxDecoration(
                                        //color: Colors.red,
                                        border: Border(
                                      bottom: BorderSide(
                                          color: Color(0xff4A4A4A4A)),
                                    )),
                                  ),
                                ),
                              ],
                            ))
         )
       ]
    );
       }
    )
    

    但对我来说,我使用 map 作为 Listview 的子项

    【讨论】:

      猜你喜欢
      • 2017-10-05
      • 2013-12-16
      • 2021-11-07
      • 2011-01-22
      • 1970-01-01
      • 2015-04-01
      • 2019-11-18
      • 1970-01-01
      相关资源
      最近更新 更多