I have a Flutter app that gets data from an SQLite database. That database has a table with a huge number of columns, so I’ve written a function that puts those column names into a list.
Therefore, columnsList
contains list of the columns in my database table.
How can I use columnsList to BOTH display the name of the column AND the VALUE of that column in a for loop?
Pseudocode
List<String> columnsList = [];
columnsList = ['RecordId', 'FirstName', 'MiddleName', 'LastName', 'Suffix', 'StreetNumber', 'StreetName'];
fetchRecord(1); // Inside initState() - returns values shown in example output
for (var i in columnsList) ...[
Text("$i: <<<VALUE OF i>>>"), // Here's where I need help
],
fetchRecord
Fetch Record looks something like this with a huge list of variables that I’d like to replace with the list.
//--- Fetch Record ---
bool isLoading = true;
List<Map<String, dynamic>> myRecord = [];
void fetchRecord(int recordId) async {
final data = await SQLiteMethods.getRecord(recordId);
setState(() {
myRecord = data;
firstName = myRecord[0]["FirstName"].toString() ?? '';
middleName = myRecord[0]["MiddleName"].toString() ?? '';
lastName = myRecord[0]["LastName"].toString() ?? '';
suffix = myRecord[0]["Suffix"].toString() ?? '';
streetNumber = myRecord[0]["StreetNumber"].toString() ?? '';
streetName = myRecord[0]["StreetName"].toString() ?? '';
isLoading = false;
});
}
Example Output
RecordId: 1
FirstName: Bob
MiddleName: E
LastName: Jones
Suffix: Mr
StreetNumber: 123
StreetName: Main St
Thanks for your help!
2
Answers
I tried to display your items from
SQLiteMethods.getRecord
withrecordId
fromWidget
.I hope this helps.
If you are getting
Map
fromSQLiteMethods.getRecord
, then you should do two changes,List<Map<String, dynamic>> myRecord = [];
toMap<String, dynamic> myRecord = {};
.Column
and lower level should befor (final key in columnsList) Text('$key: ${myRecord[key]}')
use this: