英文:
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<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(
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
答案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 = [
{
"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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论