flutter 如何< String>在for循环中使用List来生成列名和该列的值?

9nvpjoqh  于 2023-02-25  发布在  Flutter
关注(0)|答案(2)|浏览(177)

我有一个Flutter应用程序,它从SQLite数据库获取数据,该数据库有一个包含大量列的表,所以我编写了一个函数,将这些列名放入列表中。
因此,columnsList包含我的数据库表中的列的列表。
如何使用columnsList在for循环中同时显示列名和列的值?

伪代码

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 ---
  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;
    });
  }

示例输出

RecordId: 1
FirstName: Bob
MiddleName: E
LastName: Jones
Suffix: Mr
StreetNumber: 123
StreetName: Main St

谢谢你的帮忙!

mdfafbf1

mdfafbf11#

我尝试将SQLiteMethods.getRecord中的项目显示为Widget中的recordId
希望这能帮上忙。

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]}')],
          ),
        )
    ]);
  }

如果要从SQLiteMethods.getRecord获取Map,则应执行两个更改,

  1. List<Map<String, dynamic>> myRecord = [];Map<String, dynamic> myRecord = {};
    1.删除较高级别Column,较低级别应为for (final key in columnsList) Text('$key: ${myRecord[key]}')
ldxq2e6h

ldxq2e6h2#

使用此:

Column(children: [for (var i in columnsList) Text("twxt: ${i}")])

相关问题