如何获取 `list` 中每个 Map 的键和值,而无需识别键。

huangapple go评论62阅读模式
英文:

how to get list<map<string, dynamic> data each map key and value without identifying key

问题

var result = [
    {
        "SN": 1,
        "HH_NAME": "MARYAN_MAXAMED_CIISE_XUSEEN",
        "HH_NO": "BOSBAR0222598",
        "TELEPHONE": 906689662
    },
    {
        "SN": 3,
        "HH_NAME": "HANI_AADAN_CABDI",
        "HH_NO": "BOSBAR0167538",
        "TELEPHONE": 905398109
    },
    {
        "SN": 4,
        "HH_NAME": "MAXAMED_AXMED_CABDILAAHI",
        "HH_NO": "BOSBAR0192870",
        "TELEPHONE": 907923390
    },
    {
        "SN": 2,
        "HH_NAME": "HIBAAQ_AADAN_CABDI",
        "HH_NO": "BOSBAR0167539",
        "TELEPHONE": 907545689
    },
    {
        "SN": 5,
        "HH_NAME": "MAXAMED_AXMED_KILWE",
        "HH_NO": "BOSBAR0168767",
        "TELEPHONE": 907329789
    }
];
Expanded(
  child: ListView.builder(
    itemCount: result.length,
    scrollDirection: Axis.vertical,
    shrinkWrap: true,
    itemBuilder: (_, index) {
      Map map = result[index];
      return Card(
        elevation: 12,
        child: Padding(
          padding: const EdgeInsets.all(8.0),
          child: Column(
            children: [
              for (var value in map.values)
                for (var key in map.keys)
                  Align(
                    alignment: Alignment.centerLeft,
                    child: Text(
                      key.toString() +
                          ' : ' +
                          value.toString(),
                    ),
                  ),
            ],
          ),
        ),
      );
    },
  ),
),
英文:
var result = [
    {
        &quot;SN&quot;: 1,
        &quot;HH_NAME&quot;: &quot;MARYAN_MAXAMED_CIISE_XUSEEN&quot;,
        &quot;HH_NO&quot;: &quot;BOSBAR0222598&quot;,
        &quot;TELEPHONE&quot;: 906689662
    },
    {
        &quot;SN&quot;: 3,
        &quot;HH_NAME&quot;: &quot;HANI_AADAN_CABDI&quot;,
        &quot;HH_NO&quot;: &quot;BOSBAR0167538&quot;,
        &quot;TELEPHONE&quot;: 905398109
    },
    {
        &quot;SN&quot;: 4,
        &quot;HH_NAME&quot;: &quot;MAXAMED_AXMED_CABDILAAHI&quot;,
        &quot;HH_NO&quot;: &quot;BOSBAR0192870&quot;,
        &quot;TELEPHONE&quot;: 907923390
    },
    {
        &quot;SN&quot;: 2,
        &quot;HH_NAME&quot;: &quot;HIBAAQ_AADAN_CABDI&quot;,
        &quot;HH_NO&quot;: &quot;BOSBAR0167539&quot;,
        &quot;TELEPHONE&quot;: 907545689
    },
    {
        &quot;SN&quot;: 5,
        &quot;HH_NAME&quot;: &quot;MAXAMED_AXMED_KILWE&quot;,
        &quot;HH_NO&quot;: &quot;BOSBAR0168767&quot;,
        &quot;TELEPHONE&quot;: 907329789
    }
];
Expanded(
                  child: ListView.builder(
                    itemCount: results.length,
                    scrollDirection: Axis.vertical,
                    shrinkWrap: true,
                    itemBuilder: (_, index) {
                      Map map = results[index];
                      return Card(
                        elevation: 12,
                        child: Padding(
                          padding: const EdgeInsets.all(8.0),
                          child: Column(
                            children: [
                              for (var value in map.values)
                                for (var key in map.keys)
                                  Align(
                                    alignment: Alignment.centerLeft,
                                    child: Text(
                                      key.toString() +
                                          &#39; : &#39; +
                                          value.toString(),
                                    ),
                                  ),
                            ],
                          ),
                        ),
                      );
                    },
                  ),
                ),

I am doing here student results app, I converted csv to json data then I sent it to database. now I want to to get back and get all map data without identifying subjects or any other key.
if any one has more educated then me about this topic help me to do it.
many thanks.

答案1

得分: 2

不使用for循环,你可以在results[index]的条目上使用map方法,它将返回results[index]中项目的MapEntry,然后你可以使用它的keyvalue来生成你的ListView的项目。尝试这样做:

Column(
  children: map.entries.map((e) => Align(
    alignment: Alignment.centerLeft,
    child: Text(
      e.key.toString() + ' : ' + e.value.toString(),
    ),
  ),).toList()
),
英文:

Instead of for loop, you can use map method on your results[index]'s entries, it will return you MapEntry of your items inside results[index], then you can use its key and value to generate your listview's items. Try this:

Column(
    children: map.entries.map((e) =&gt; Align(
            alignment: Alignment.centerLeft,
            child: Text(
              e.key.toString() + &#39; : &#39; + e.value.toString(),
            ),
          ),).toList()
  ),

huangapple
  • 本文由 发表于 2023年2月9日 02:17:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75390103.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定