How to add dynamically items to the table in flutter?
Do someone know how to add dynamically more rows into the DataTable in Flutter. As you can see my code is very 'hardcoded' [line: 11-31].
There should be a way to get rid of writing more and more DataRows.
Code:
class DataTableWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return DataTable(
columns: [
DataColumn(label: Text('Patch')),
DataColumn(label: Text('Version')),
DataColumn(label: Text('Ready')),
],
rows: <DataRow>[
DataRow(
cells: <DataCell>[
DataCell(Text('AAAAAA')),
DataCell(Text('1')),
DataCell(Text('Yes')),
],
),
DataRow(
cells: <DataCell>[
DataCell(Text('BBBBBB')),
DataCell(Text('2')),
DataCell(Text('No')),
],
),
DataRow(
cells: <DataCell>[
DataCell(Text('CCCCCC')),
DataCell(Text('3')),
DataCell(Text('Yes')),
],
),
],
);
}
}
dart flutter
add a comment |
Do someone know how to add dynamically more rows into the DataTable in Flutter. As you can see my code is very 'hardcoded' [line: 11-31].
There should be a way to get rid of writing more and more DataRows.
Code:
class DataTableWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return DataTable(
columns: [
DataColumn(label: Text('Patch')),
DataColumn(label: Text('Version')),
DataColumn(label: Text('Ready')),
],
rows: <DataRow>[
DataRow(
cells: <DataCell>[
DataCell(Text('AAAAAA')),
DataCell(Text('1')),
DataCell(Text('Yes')),
],
),
DataRow(
cells: <DataCell>[
DataCell(Text('BBBBBB')),
DataCell(Text('2')),
DataCell(Text('No')),
],
),
DataRow(
cells: <DataCell>[
DataCell(Text('CCCCCC')),
DataCell(Text('3')),
DataCell(Text('Yes')),
],
),
],
);
}
}
dart flutter
add a comment |
Do someone know how to add dynamically more rows into the DataTable in Flutter. As you can see my code is very 'hardcoded' [line: 11-31].
There should be a way to get rid of writing more and more DataRows.
Code:
class DataTableWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return DataTable(
columns: [
DataColumn(label: Text('Patch')),
DataColumn(label: Text('Version')),
DataColumn(label: Text('Ready')),
],
rows: <DataRow>[
DataRow(
cells: <DataCell>[
DataCell(Text('AAAAAA')),
DataCell(Text('1')),
DataCell(Text('Yes')),
],
),
DataRow(
cells: <DataCell>[
DataCell(Text('BBBBBB')),
DataCell(Text('2')),
DataCell(Text('No')),
],
),
DataRow(
cells: <DataCell>[
DataCell(Text('CCCCCC')),
DataCell(Text('3')),
DataCell(Text('Yes')),
],
),
],
);
}
}
dart flutter
Do someone know how to add dynamically more rows into the DataTable in Flutter. As you can see my code is very 'hardcoded' [line: 11-31].
There should be a way to get rid of writing more and more DataRows.
Code:
class DataTableWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return DataTable(
columns: [
DataColumn(label: Text('Patch')),
DataColumn(label: Text('Version')),
DataColumn(label: Text('Ready')),
],
rows: <DataRow>[
DataRow(
cells: <DataCell>[
DataCell(Text('AAAAAA')),
DataCell(Text('1')),
DataCell(Text('Yes')),
],
),
DataRow(
cells: <DataCell>[
DataCell(Text('BBBBBB')),
DataCell(Text('2')),
DataCell(Text('No')),
],
),
DataRow(
cells: <DataCell>[
DataCell(Text('CCCCCC')),
DataCell(Text('3')),
DataCell(Text('Yes')),
],
),
],
);
}
}
dart flutter
dart flutter
edited Jan 1 at 17:35
Brian Tompsett - 汤莱恩
4,2331338101
4,2331338101
asked Jan 1 at 17:29
Sarah PöhlerSarah Pöhler
71110
71110
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You can use
listOfColumns.map(((element) => DataRow(...))).toList()
This is your code using this method.
class DataTableWidget extends StatelessWidget {
final List<Map<String, String>> listOfColumns = [
{"Name": "AAAAAA", "Number": "1", "State": "Yes"},
{"Name": "BBBBBB", "Number": "2", "State": "no"},
{"Name": "CCCCCC", "Number": "3", "State": "Yes"}
];
// DataTableWidget(this.listOfColumns); // Getting the data from outside, on initialization
@override
Widget build(BuildContext context) {
return DataTable(
columns: [
DataColumn(label: Text('Patch')),
DataColumn(label: Text('Version')),
DataColumn(label: Text('Ready')),
],
rows:
listOfColumns // Loops through dataColumnText, each iteration assigning the value to element
.map(
((element) => DataRow(
cells: <DataCell>[
DataCell(Text(element["Name"])), //Extracting from Map element the value
DataCell(Text(element["Number"])),
DataCell(Text(element["State"])),
],
)),
)
.toList(),
);
}
}
this is not what I want. I just want to enter the size of the table (row and column) and get a datatable oder table. there should be a common way for it.
– Sarah Pöhler
Feb 2 at 0:53
add a comment |
You could do something like this:
class DataTableWidget extends StatelessWidget {
List<Map> someData = [
{
"text1": "AAAA",
"text2": "1",
"text3": "YES",
},
{
"text1": "BBBB",
"text2": "2",
"text3": "NO",
},
{
"text1": "CCCC",
"text2": "3",
"text3": "YES",
},
];
DataRow _getDataRow(data) {
return DataRow(
cells: <DataCell>[
DataCell(Text(data["text1"])),
DataCell(Text(data["text2"])),
DataCell(Text(data["text3"])),
],
);
}
@override
Widget build(BuildContext context) {
return DataTable(
columns: [
DataColumn(label: Text('Patch')),
DataColumn(label: Text('Version')),
DataColumn(label: Text('Ready')),
],
rows: List.generate(
someData.length, (index) => _getDataRow(someData[index])),
);
}
}
why is the list empty? The code does not show text1, text2 and text3
– Sarah Pöhler
Jan 1 at 18:24
@SarahPöhler I left the list empty, because I don't know if you are getting the data from a server or local database or something else. Populating the list is not the point of this question. But since you don't seem satisfied, I edited my answer
– dshukertjr
Jan 2 at 3:51
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53997496%2fhow-to-add-dynamically-items-to-the-table-in-flutter%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use
listOfColumns.map(((element) => DataRow(...))).toList()
This is your code using this method.
class DataTableWidget extends StatelessWidget {
final List<Map<String, String>> listOfColumns = [
{"Name": "AAAAAA", "Number": "1", "State": "Yes"},
{"Name": "BBBBBB", "Number": "2", "State": "no"},
{"Name": "CCCCCC", "Number": "3", "State": "Yes"}
];
// DataTableWidget(this.listOfColumns); // Getting the data from outside, on initialization
@override
Widget build(BuildContext context) {
return DataTable(
columns: [
DataColumn(label: Text('Patch')),
DataColumn(label: Text('Version')),
DataColumn(label: Text('Ready')),
],
rows:
listOfColumns // Loops through dataColumnText, each iteration assigning the value to element
.map(
((element) => DataRow(
cells: <DataCell>[
DataCell(Text(element["Name"])), //Extracting from Map element the value
DataCell(Text(element["Number"])),
DataCell(Text(element["State"])),
],
)),
)
.toList(),
);
}
}
this is not what I want. I just want to enter the size of the table (row and column) and get a datatable oder table. there should be a common way for it.
– Sarah Pöhler
Feb 2 at 0:53
add a comment |
You can use
listOfColumns.map(((element) => DataRow(...))).toList()
This is your code using this method.
class DataTableWidget extends StatelessWidget {
final List<Map<String, String>> listOfColumns = [
{"Name": "AAAAAA", "Number": "1", "State": "Yes"},
{"Name": "BBBBBB", "Number": "2", "State": "no"},
{"Name": "CCCCCC", "Number": "3", "State": "Yes"}
];
// DataTableWidget(this.listOfColumns); // Getting the data from outside, on initialization
@override
Widget build(BuildContext context) {
return DataTable(
columns: [
DataColumn(label: Text('Patch')),
DataColumn(label: Text('Version')),
DataColumn(label: Text('Ready')),
],
rows:
listOfColumns // Loops through dataColumnText, each iteration assigning the value to element
.map(
((element) => DataRow(
cells: <DataCell>[
DataCell(Text(element["Name"])), //Extracting from Map element the value
DataCell(Text(element["Number"])),
DataCell(Text(element["State"])),
],
)),
)
.toList(),
);
}
}
this is not what I want. I just want to enter the size of the table (row and column) and get a datatable oder table. there should be a common way for it.
– Sarah Pöhler
Feb 2 at 0:53
add a comment |
You can use
listOfColumns.map(((element) => DataRow(...))).toList()
This is your code using this method.
class DataTableWidget extends StatelessWidget {
final List<Map<String, String>> listOfColumns = [
{"Name": "AAAAAA", "Number": "1", "State": "Yes"},
{"Name": "BBBBBB", "Number": "2", "State": "no"},
{"Name": "CCCCCC", "Number": "3", "State": "Yes"}
];
// DataTableWidget(this.listOfColumns); // Getting the data from outside, on initialization
@override
Widget build(BuildContext context) {
return DataTable(
columns: [
DataColumn(label: Text('Patch')),
DataColumn(label: Text('Version')),
DataColumn(label: Text('Ready')),
],
rows:
listOfColumns // Loops through dataColumnText, each iteration assigning the value to element
.map(
((element) => DataRow(
cells: <DataCell>[
DataCell(Text(element["Name"])), //Extracting from Map element the value
DataCell(Text(element["Number"])),
DataCell(Text(element["State"])),
],
)),
)
.toList(),
);
}
}
You can use
listOfColumns.map(((element) => DataRow(...))).toList()
This is your code using this method.
class DataTableWidget extends StatelessWidget {
final List<Map<String, String>> listOfColumns = [
{"Name": "AAAAAA", "Number": "1", "State": "Yes"},
{"Name": "BBBBBB", "Number": "2", "State": "no"},
{"Name": "CCCCCC", "Number": "3", "State": "Yes"}
];
// DataTableWidget(this.listOfColumns); // Getting the data from outside, on initialization
@override
Widget build(BuildContext context) {
return DataTable(
columns: [
DataColumn(label: Text('Patch')),
DataColumn(label: Text('Version')),
DataColumn(label: Text('Ready')),
],
rows:
listOfColumns // Loops through dataColumnText, each iteration assigning the value to element
.map(
((element) => DataRow(
cells: <DataCell>[
DataCell(Text(element["Name"])), //Extracting from Map element the value
DataCell(Text(element["Number"])),
DataCell(Text(element["State"])),
],
)),
)
.toList(),
);
}
}
answered Jan 3 at 16:33
guyguy
603113
603113
this is not what I want. I just want to enter the size of the table (row and column) and get a datatable oder table. there should be a common way for it.
– Sarah Pöhler
Feb 2 at 0:53
add a comment |
this is not what I want. I just want to enter the size of the table (row and column) and get a datatable oder table. there should be a common way for it.
– Sarah Pöhler
Feb 2 at 0:53
this is not what I want. I just want to enter the size of the table (row and column) and get a datatable oder table. there should be a common way for it.
– Sarah Pöhler
Feb 2 at 0:53
this is not what I want. I just want to enter the size of the table (row and column) and get a datatable oder table. there should be a common way for it.
– Sarah Pöhler
Feb 2 at 0:53
add a comment |
You could do something like this:
class DataTableWidget extends StatelessWidget {
List<Map> someData = [
{
"text1": "AAAA",
"text2": "1",
"text3": "YES",
},
{
"text1": "BBBB",
"text2": "2",
"text3": "NO",
},
{
"text1": "CCCC",
"text2": "3",
"text3": "YES",
},
];
DataRow _getDataRow(data) {
return DataRow(
cells: <DataCell>[
DataCell(Text(data["text1"])),
DataCell(Text(data["text2"])),
DataCell(Text(data["text3"])),
],
);
}
@override
Widget build(BuildContext context) {
return DataTable(
columns: [
DataColumn(label: Text('Patch')),
DataColumn(label: Text('Version')),
DataColumn(label: Text('Ready')),
],
rows: List.generate(
someData.length, (index) => _getDataRow(someData[index])),
);
}
}
why is the list empty? The code does not show text1, text2 and text3
– Sarah Pöhler
Jan 1 at 18:24
@SarahPöhler I left the list empty, because I don't know if you are getting the data from a server or local database or something else. Populating the list is not the point of this question. But since you don't seem satisfied, I edited my answer
– dshukertjr
Jan 2 at 3:51
add a comment |
You could do something like this:
class DataTableWidget extends StatelessWidget {
List<Map> someData = [
{
"text1": "AAAA",
"text2": "1",
"text3": "YES",
},
{
"text1": "BBBB",
"text2": "2",
"text3": "NO",
},
{
"text1": "CCCC",
"text2": "3",
"text3": "YES",
},
];
DataRow _getDataRow(data) {
return DataRow(
cells: <DataCell>[
DataCell(Text(data["text1"])),
DataCell(Text(data["text2"])),
DataCell(Text(data["text3"])),
],
);
}
@override
Widget build(BuildContext context) {
return DataTable(
columns: [
DataColumn(label: Text('Patch')),
DataColumn(label: Text('Version')),
DataColumn(label: Text('Ready')),
],
rows: List.generate(
someData.length, (index) => _getDataRow(someData[index])),
);
}
}
why is the list empty? The code does not show text1, text2 and text3
– Sarah Pöhler
Jan 1 at 18:24
@SarahPöhler I left the list empty, because I don't know if you are getting the data from a server or local database or something else. Populating the list is not the point of this question. But since you don't seem satisfied, I edited my answer
– dshukertjr
Jan 2 at 3:51
add a comment |
You could do something like this:
class DataTableWidget extends StatelessWidget {
List<Map> someData = [
{
"text1": "AAAA",
"text2": "1",
"text3": "YES",
},
{
"text1": "BBBB",
"text2": "2",
"text3": "NO",
},
{
"text1": "CCCC",
"text2": "3",
"text3": "YES",
},
];
DataRow _getDataRow(data) {
return DataRow(
cells: <DataCell>[
DataCell(Text(data["text1"])),
DataCell(Text(data["text2"])),
DataCell(Text(data["text3"])),
],
);
}
@override
Widget build(BuildContext context) {
return DataTable(
columns: [
DataColumn(label: Text('Patch')),
DataColumn(label: Text('Version')),
DataColumn(label: Text('Ready')),
],
rows: List.generate(
someData.length, (index) => _getDataRow(someData[index])),
);
}
}
You could do something like this:
class DataTableWidget extends StatelessWidget {
List<Map> someData = [
{
"text1": "AAAA",
"text2": "1",
"text3": "YES",
},
{
"text1": "BBBB",
"text2": "2",
"text3": "NO",
},
{
"text1": "CCCC",
"text2": "3",
"text3": "YES",
},
];
DataRow _getDataRow(data) {
return DataRow(
cells: <DataCell>[
DataCell(Text(data["text1"])),
DataCell(Text(data["text2"])),
DataCell(Text(data["text3"])),
],
);
}
@override
Widget build(BuildContext context) {
return DataTable(
columns: [
DataColumn(label: Text('Patch')),
DataColumn(label: Text('Version')),
DataColumn(label: Text('Ready')),
],
rows: List.generate(
someData.length, (index) => _getDataRow(someData[index])),
);
}
}
edited Jan 2 at 3:52
answered Jan 1 at 17:37
dshukertjrdshukertjr
1,8711828
1,8711828
why is the list empty? The code does not show text1, text2 and text3
– Sarah Pöhler
Jan 1 at 18:24
@SarahPöhler I left the list empty, because I don't know if you are getting the data from a server or local database or something else. Populating the list is not the point of this question. But since you don't seem satisfied, I edited my answer
– dshukertjr
Jan 2 at 3:51
add a comment |
why is the list empty? The code does not show text1, text2 and text3
– Sarah Pöhler
Jan 1 at 18:24
@SarahPöhler I left the list empty, because I don't know if you are getting the data from a server or local database or something else. Populating the list is not the point of this question. But since you don't seem satisfied, I edited my answer
– dshukertjr
Jan 2 at 3:51
why is the list empty? The code does not show text1, text2 and text3
– Sarah Pöhler
Jan 1 at 18:24
why is the list empty? The code does not show text1, text2 and text3
– Sarah Pöhler
Jan 1 at 18:24
@SarahPöhler I left the list empty, because I don't know if you are getting the data from a server or local database or something else. Populating the list is not the point of this question. But since you don't seem satisfied, I edited my answer
– dshukertjr
Jan 2 at 3:51
@SarahPöhler I left the list empty, because I don't know if you are getting the data from a server or local database or something else. Populating the list is not the point of this question. But since you don't seem satisfied, I edited my answer
– dshukertjr
Jan 2 at 3:51
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53997496%2fhow-to-add-dynamically-items-to-the-table-in-flutter%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown