从Flutter中检索第二个索引数据。

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

Retrieve data from the second index data in flutter

问题

class DataList {
  static List<Map<String, dynamic>> computerData = [
    {
      "maintopic": "Computer Software",
      "subtopic": [
        {
          "topic": "introduction",
          "content": "owhrfiuwbef iosevibskvsuibvisncsd nisudvusvc"
        }
      ]
    },
    {
      "maintopic": "Computer",
      "subtopic": [
        {
          "topic": "introduction",
          "content": "owhrfic"
        }
      ]
    }
  ];
}

// How do I access the content key from the above complex List<Map> placed in the second position?

ListView.builder(
  itemCount: DataList.computerData.length,
  itemBuilder: (context, index) {
    return Container(
      child: Column(
        children: [
          Text(DataList.computerData[index]["subtopic"][0]["content"]),
          // Accessing the content key from the second position's subtopic list
        ],
      ),
    );
  },
);

请注意,我已经翻译了您提供的代码部分,但保留了代码的结构和标识符不变。如果您有任何问题或需要进一步的帮助,请随时告诉我。

英文:
class DataList {
static List&lt;Map&lt;String, dynamic&gt;&gt; computerData =
[
{
&quot;maintopic&quot;: &quot;Computer Software&quot;,
&quot;subtopic&quot;: [
{
&quot;topic&quot;: &quot;introduction&quot;,
&quot;content&quot;: &quot;owhrfiuwbef iosevibskvsuibvisncsd
nisudvusvc
}
]
},
{
&quot;maintopic&quot;: &quot;Computer&quot;,
&quot;subtopic&quot;: [
{
&quot;topic&quot;: &quot;introduction&quot;, &quot;content&quot;: &quot;owhrfic&quot;
},
]
}

how do i access the content key from the above complex List<Map> placed in the second position?

ListView.builder(
   DataList.computerData.length, itemBuilder: (context, index) {
    return Container(
    child: Column(
    children: [
    DataList.computerData[index][&quot;subtopic&quot;], 
    ...DataList.computerData[index][&quot;subtopic&quot;]
    .forEach((data) {
    data[&#39;subtopic&#39;].forEach((subtopic) {
    }),

i tried the code above but still unable to get the required data

答案1

得分: 1

以下是翻译好的代码部分:

class MyPage extends StatelessWidget {
  MyPage({super.key});
  final computerData = [
    {
      "maintopic": "计算机软件",
      "subtopic": [
        {
          "topic": "介绍",
          "content": "owhrfiuwbef iosevibskvsuibvisnesd nisudvusvc",
        }
      ]
    },
    {
      "maintopic": "计算机",
      "subtopic": [
        {
          "topic": "介绍",
          "content": "owhrfic",
        }
      ]
    }
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView.builder(
        shrinkWrap: true,
        physics: const ClampingScrollPhysics(),
        itemCount: computerData.length,
        itemBuilder: ((context, index) {
          final List<Map> arrSubTopic =
              computerData[index]['subtopic'] as List<Map>;
          return Column(
            children: [
              Container(
                height: 40,
                color: Colors.red,
                child: Row(
                  children: [
                    Expanded(
                      child: Center(
                        child:
                            Text(computerData[index]['maintopic'] as String),
                      ),
                    )
                  ],
                ),
              ),
              Container(
                color: Colors.amber,
                child: ListView.builder(
                  shrinkWrap: true,
                  physics: const ClampingScrollPhysics(),
                  itemCount: arrSubTopic.length,
                  itemBuilder: ((context, inx) {
                    return Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(arrSubTopic[inx]['topic'] as String),
                          Text(arrSubTopic[inx]['content'] as String)
                        ],
                      ),
                    );
                  }),
                ),
              )
            ],
          );
        }),
      ),
    );
  }
}

希望这对您有所帮助!

英文:

try this code:

class MyPage extends StatelessWidget {
  MyPage({super.key});
  final coumputerData = [
    {
      &quot;maintopic&quot;: &quot;Computer Software&quot;,
      &quot;subtopic&quot;: [
        {
          &quot;topic&quot;: &quot;introduction&quot;,
          &quot;content&quot;: &quot;owhrfiuwbef iosevibskvsuibvisnesd nisudvusvc&quot;,
        }
      ]
    },
    {
      &quot;maintopic&quot;: &quot;Computer&quot;,
      &quot;subtopic&quot;: [
        {
          &quot;topic&quot;: &quot;introduction&quot;,
          &quot;content&quot;: &quot;owhrfic&quot;,
        }
      ]
    }
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView.builder(
        shrinkWrap: true,
        physics: const ClampingScrollPhysics(),
        itemCount: coumputerData.length,
        itemBuilder: ((context, index) {
          final List&lt;Map&gt; arrSubTopic =
              coumputerData[index][&#39;subtopic&#39;] as List&lt;Map&gt;;
          return Column(
            children: [
              Container(
                height: 40,
                color: Colors.red,
                child: Row(
                  children: [
                    Expanded(
                      child: Center(
                        child:
                            Text(coumputerData[index][&#39;maintopic&#39;] as String),
                      ),
                    )
                  ],
                ),
              ),
              Container(
                color: Colors.amber,
                child: ListView.builder(
                  shrinkWrap: true,
                  physics: const ClampingScrollPhysics(),
                  itemCount: arrSubTopic.length,
                  itemBuilder: ((context, inx) {
                    return Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(arrSubTopic[inx][&#39;topic&#39;] as String),
                          Text(arrSubTopic[inx][&#39;content&#39;] as String)
                        ],
                      ),
                    );
                  }),
                ),
              )
            ],
          );
        }),
      ),
    );
  }
}

You can also access your data using model class. to generate model class you can use this website.

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

发表评论

匿名网友

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

确定