How to solve: who to iterate mixed List and Map and object in dart and flutter

Question

Asked by user3819947 on September 21, 2022 (source).

how can i iterate bello list and show it in my structure?

List courses = [
  {'course 1':{'p':['course 0'],'need':[],'v':3}},
  {'course 2':{'p':[],'need':['course 1','course 0'],'v':2}},
  {'course 3':{'p':['course 1'],'need':[],'v':2}},
];


@override
  Widget build(BuildContext context) {
    return Container(

      child: Column(children: [
      ...courses.map((e) => Container(
        color: Colors.amber,
        width: 200,
        height: 40,
        margin: EdgeInsets.all(4),
        child: Column(

        children: [
          Text(e[0],style: TextStyle(color: Colors.white),),
          Text(e['p'],style: TextStyle(color: Colors.grey),),
          Text(e['need'],style: TextStyle(color: Colors.grey),),
          Text(e['v'],style: TextStyle(color: Colors.grey),),

        ],
      ),)),

    ],),);
  }

i want my result be like this:

course 1 : p: course 0 / need: null

course 2 : p: null / need: course 1 + course 0

. . .

thanks .

Answer

Question answered by Ivo (source).

Maybe something like this is what you want:

@override
Widget build(BuildContext context) {
  return Container(
    child: Column(children: [
      ...courses.map((e) {
        final entry = e.entries.first;
        return Container(
        color: Colors.amber,
        width: 200,
        margin: EdgeInsets.all(4),
        child: Column(
          children: [
            Text(entry.key,style: TextStyle(color: Colors.white),),
            Text(entry.value['p'].length == 0 ? 'null' : entry.value['p'].join(' + '),style: TextStyle(color: Colors.grey),),
            Text(entry.value['need'].length == 0 ? 'null' : entry.value['need'].join(' + '),style: TextStyle(color: Colors.grey),),
            Text(entry.value['v'].toString(),style: TextStyle(color: Colors.grey),),
          ],
        ),);}),

    ],),);
}
DART DICTIONARY FLUTTER LIST LOOPS
SHARE: