Reputation: 361
I want to show the contents of a Map<String, String> as table in my widget. How can I iterate the Map? I am not sure if I could do this inside the Widget or if I have to do it "outside":
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
],
),
),
),
),
);
}
Thanks a lot for help in advance!
Upvotes: 1
Views: 1627
Reputation: 8393
Just map the entries of your Map<String, String>
to your DataRows
and DataCells
:
rows: Test.myMap.entries
.map(
(entry) => DataRow(
cells: [
DataCell(Text(entry.key)),
DataCell(Text(entry.value)),
],
),
)
.toList(),
Upvotes: 3