开发者社区> 问答> 正文

Flutter List <动态>应为List <DataRow>

我正在尝试使用flutter的DataTable小部件,但一直出现此错误,我想创建一种方法来为表格生成数据。


var dataList = [
    DataSectionCollection(
      category: 'Category',
      date: 'January 01, 2019',
      item: [
        DataSectionItem(
            symbol: "ICMS", amount: " 474.858.228.17", percentage: 3.55),
        DataSectionItem(
            symbol: "ICMS", amount: " 474.858.228.17", percentage: 3.55),
        DataSectionItem(
            symbol: "ICMS", amount: " 474.858.228.17", percentage: 3.55),
        DataSectionItem(
            symbol: "ICMS", amount: " 474.858.228.17", percentage: 3.55),
      ],
    ),
    DataSectionCollection(
      category: 'Category',
      date: 'January 01, 2019',
      item: [
        DataSectionItem(
            symbol: "AAAA", amount: " 474.858.228.17", percentage: 3.55),
        DataSectionItem(
            symbol: "AAA", amount: " 474.858.228.17", percentage: 3.55),
        DataSectionItem(
            symbol: "AAA", amount: " 474.858.228.17", percentage: 3.55),
        DataSectionItem(
            symbol: "AAA", amount: " 474.858.228.17", percentage: 3.55),
      ],
    ),
    DataSectionCollection(
      category: 'Category',
      date: 'January 01, 2019',
      item: [
        DataSectionItem(
            symbol: "BBBB", amount: " 474.858.228.17", percentage: 3.55),
        DataSectionItem(
            symbol: "BBBB", amount: " 474.858.228.17", percentage: 3.55),
        DataSectionItem(
            symbol: "BBBB", amount: " 474.858.228.17", percentage: 3.55),
        DataSectionItem(
            symbol: "BBBB", amount: " 474.858.228.17", percentage: 3.55),
      ],
    ),
  ];

  _getData01(List listOfData) {
    return DataTable(
      columns: listOfData
          .map(
            (column) => DataColumn(
              label: Container(),
            ),
          )
          .toList(),
      rows: listOfData
          .map((stat) => stat.item.map((row) => DataRow(cells: [
                DataCell(
                  Text(row.symbol),
                ),
                DataCell(
                  Text(row.amount),
                ),
                DataCell(
                  Text("${row.symbol}"),
                ),
              ])))
          .toList(),
    );
  }

我不确定自己在做什么错。有人可以帮忙吗?我在列表上映射以创建列的第一位到第二位都工作正常。

展开
收起
Puppet 2020-01-17 09:07:10 1494 0
1 条回答
写回答
取消 提交回答
  • 您的问题在于.map()遍历数据时方法的嵌套。我将其弄乱以使其更具可读性并交换为.forEach()方法:

    
    Widget _getData01(List listOfData) {
      List<DataRow> rows = [];
    
      listOfData.forEach((stat){
        stat.item.forEach((row){
          rows.add(
            DataRow(
              cells: [
                DataCell(
                  Text(row.symbol),
                ),
                DataCell(
                  Text(row.amount),
                ),
                DataCell(
                  Text("${row.symbol}"),
                ),
              ]
            )
          );
        });
      });
    
      return DataTable(
        columns: listOfData.map(
          (column) => DataColumn(
            label: Container(),
          )
        ).toList(),
        rows: rows,
      );
    }
    

    我只能通过用自己的数据替换您的模型类来进行测试,因此请进行测试并提供反馈。

    2020-01-17 09:07:35
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
基于flutter的产品应用实践 立即下载
《Flutter in action》 立即下载
闲鱼《Flutter 技术解析与实战》 立即下载