Home > OS >  who to iterate mixed List and Map and object in dart and flutter
who to iterate mixed List and Map and object in dart and flutter

Time:09-21

who 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 .

CodePudding user response:

I solved your problem but you can change the output and get what you need:

void courseFinder() {
  String? convertList(List myList) {
    // convert list and join by  
    if (myList.isNotEmpty) {
      return myList.join("   ");
    }
    return null;
  }

  for (Map element in courses) {
    element.forEach((key, value) {
      String? p = convertList(value["p"]);
      String? need = convertList(value["need"]);
      // this is what you need you can return whare you want or change it into list and use it
      print("${key} : p: ${p} / need: ${need}"); 
    });
  }
}

CodePudding user response:

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),),
          ],
        ),);}),

    ],),);
}

CodePudding user response:

You should propably use a class instead of a Map. List make more sense and is easier to deal with now and later on.

Course class :

class Course
{
  final String name;
  final List<String> p;
  final List<String> need;
  final int v;
  
  Course(this.name, this.p, this.need, this.v);
} 

Builder :

   List courses = [
        Course('course 1',['course 0'],[],3),
        Course('course 2',[],['course 1','course 0'],2),
        Course('course 3',['course 1'],[],2),
      ];

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

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

        children: [
          Text(course.name,style: TextStyle(color: Colors.white),),
          Text(course.p.join(","),style: TextStyle(color: Colors.grey),),
          Text(course.need.join(","),style: TextStyle(color: Colors.grey),),
          Text(course.v,style: TextStyle(color: Colors.grey),),

        ],
      ),)),

    ],),);
  }
  • Related