skip to Main Content

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


  1. I tried to display your items from SQLiteMethods.getRecord with recordId from Widget.

    I hope this helps.

    class YourWidget extends StatefulWidget {
      const YourWidget({Key? key, required this.recordId}) : super(key: key);
      final int recordId;
    
      @override
      State<YourWidget> createState() => _YourWidgetState();
    }
    
    class _YourWidgetState extends State<YourWidget> {
      List<Map<String, dynamic>> myRecord = [];
      final List<String> columnsList = [
        'RecordId',
        'FirstName',
        'MiddleName',
        'LastName',
        'Suffix',
        'StreetNumber',
        'StreetName'
      ];
      bool isLoading = false;
    
      @override
      void initState() {
        super.initState();
        fetRecord(widget.recordId);
      }
    
      Future<void> fetRecord(int recordId) async {
        isLoading = true;
        myRecord = await SQLiteMethods.getRecord(recordId);
        isLoading = false;
        setState(() {});
      }
    
      @override
      Widget build(BuildContext context) {
        if (isLoading) {
          return const Center(
            child: CircularProgressIndicator(),
          );
        }
        return Column(children: [
          for (final record in myRecord)
            Container(
              color: Colors.black12,
              padding: const EdgeInsets.all(10),
              child: Column(
                children: [for (final key in columnsList) Text('$key: ${record[key]}')],
              ),
            )
        ]);
      }
    

    If you are getting Map from SQLiteMethods.getRecord, then you should do two changes,

    1. List<Map<String, dynamic>> myRecord = []; to Map<String, dynamic> myRecord = {};.
    2. Remove the higher level Column and lower level should be for (final key in columnsList) Text('$key: ${myRecord[key]}')
    Login or Signup to reply.
  2. use this:

    Column(children: [for (var i in columnsList) Text("twxt: ${i}")])
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search