Saman Kc
Saman Kc

Reputation: 149

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 placed in the second position?

ListView.builder(
   DataList.computerData.length, itemBuilder: (context, index) {
    return Container(
    child: Column(
    children: [
    DataList.computerData[index]["subtopic"], 
    ...DataList.computerData[index]["subtopic"]
    .forEach((data) {
    data['subtopic'].forEach((subtopic) {
    }),

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

Upvotes: 0

Views: 82

Answers (1)

Vishal Zaveri
Vishal Zaveri

Reputation: 1557

try this code:

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView.builder(
        shrinkWrap: true,
        physics: const ClampingScrollPhysics(),
        itemCount: coumputerData.length,
        itemBuilder: ((context, index) {
          final List<Map> arrSubTopic =
              coumputerData[index]['subtopic'] as List<Map>;
          return Column(
            children: [
              Container(
                height: 40,
                color: Colors.red,
                child: Row(
                  children: [
                    Expanded(
                      child: Center(
                        child:
                            Text(coumputerData[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)
                        ],
                      ),
                    );
                  }),
                ),
              )
            ],
          );
        }),
      ),
    );
  }
}

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

Upvotes: 1

Related Questions