Flutter/Dart:从 Map<String, String> 获取数据行并在小部件中显示为 table

Flutter/Dart: Getting DataRows from Map<String, String> and show them as table in a widget

我想在我的小部件中将 Map 的内容显示为 table。我怎样才能迭代地图? 我不确定我是否可以在 Widget 内部执行此操作,或者我是否必须在“外部”执行此操作:

class Test extends StatefulWidget {
  Test();
  static Map<String, String> myMap = {
    "col1 row1": "some value",
    "col1 row2": "another value",
    "col1 row3": "foo-value"
  };

  @override
  _TestState createState() => _TestState();
}

class _TestState extends State<Test> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Some title'),
      ),
      body: SafeArea(
        child: Center(
          child: Scrollbar(
            child: DataTable(
              columns: [
                DataColumn(
                  label: Text('Col 1'),
                ),
                DataColumn(
                  label: Text('Col 2'),
                ),
              ],
              rows: [
                // Want to show the map-data here, like
                // col1 row1 | some value
                // col1 row2 | another value
                // col1 row3 | foo-value
              ],
            ),
          ),
        ),
      ),
    );
  }

非常感谢您的提前帮助!

只需将 Map<String, String> 的条目映射到 DataRowsDataCells:

rows: Test.myMap.entries
  .map(
    (entry) => DataRow(
      cells: [
        DataCell(Text(entry.key)),
        DataCell(Text(entry.value)),
      ],
    ),
  )
  .toList(),