Flutter List<Dynamic> 应该是 List<DataRow>

Sat*_*an 1 android ios flutter

我正在尝试使用 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(),
    );
  }
Run Code Online (Sandbox Code Playgroud)

我不完全确定我做错了什么。有人可以帮忙吗?我映射列表以创建列的第一位工作正常,但第二位却没有。

Joã*_*res 6

您的问题在于.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,
  );
}
Run Code Online (Sandbox Code Playgroud)

我只能通过用我自己的数据替换您的模型类来测试这一点,所以请测试它并给我反馈。