Home > Software engineering >  How to convert a grouped map to list in Flutter
How to convert a grouped map to list in Flutter

Time:09-27

I have this grouped map. I need to convert this map to a list for using this inside a listview builder.

        Plants Around Us: [
    {sequenceno: 193, 
    _id: 5f041, 
    chapter: Plants Around Us, 
    title: Plants Around Us, 
    package_description: In this module, let us discuss about different types of plants and about different places they live.,
     age_level: [99, 6], 
    pkg_sequence: 100, 
    packagescors: []},
         {sequenceno: 193,
     _id: 5ec50, 
    chapter: Plants Around Us,
     title: Activity: Types of Plants,
     package_description: Let us go through some activities based on the lesson ‘Types of Plants’., 
    age_level: [99, 6], 
    pkg_sequence: 101, 
    packagescors: []}
],
Leaves and Flowers: [
{sequenceno: 193,
 _id: 5c48, 
chapter: Leaves and Flowers, 
title: Leaves and Flowers,
 package_description: In this article,
 age_level: [99, 6], 
pkg_sequence: 100,
 packagescors: []},

How can I convert this grouped map to a List?

Model for the grouped data

class Chapter {
  String? chapter;
  List<Title>?title;

  Chapter({this.chapter,this.title});
  factory Chapter.fromMap(Map<String, dynamic> map) {
    return Chapter(
      chapter: map['chapter'],
      title: map["title"] != null
          ? List<Title>.from(
              map["title"].map((x) => Title.fromMap(x)))
          : [],
    );
  }
  Map<String, dynamic> toMap(){
      var map = <String,dynamic>{
      };
       return map;
      }
}

Title Model

class Title{
  String? sequenceno;
  String? id;
  String? chapter;
   String? title;
  String? packageDescription;
  String? ageLevel;
  String? pkgSequence;
  String? packagescors;
  Title({
    this.sequenceno,
    this.id,
    this.chapter,
    this.title,
    this.packageDescription,
    this.ageLevel,
    this.pkgSequence,
    this.packagescors,
  });
  factory Title.fromMap(Map<String, dynamic> map) {
    return Title(
      sequenceno: map['sequenceno'],
      id: map["_id"],
      chapter: map['chapter'],
      title: map['title'],
      packageDescription: map['package_description'],
      ageLevel: map['age_level'],
      pkgSequence: map['pkg_sequence'],
      // packagescors: map['packagescors'],
    );
}
Map<String, dynamic> toMap(){
      var map = <String,dynamic>{
        "sequenceno": sequenceno,
        "_id": id,
        "chapter": chapter,
        "title": title,
        "package_description": packageDescription,
        "age_level": ageLevel,
        "pkg_sequence": pkgSequence,
        // "packagescors": packagescors,
      };
       return map;
      }}

I created this model, because of the 'Chapter' is how its grouped and 'Title' Contains Multiple values all the related data of including id, and all other values

CodePudding user response:

You could simply do this

void main() {
  List<Map<String, List>> theList = [];
  theList.add({"Leaves and Flowers" : [{" _id": "5c48", "chapter": "Leaves and Flowers", "age_level": [99, 6]}]});
  print(theList);

  // output would be
  // [{Leaves and Flowers: [{ _id: 5c48, chapter: Leaves and Flowers, age_level: [99, 6]}]}]
}

But it can tend to get real messy, I suggest you take a look at classes & objects.

class LeafModel {
  String _id = "";
  String chapter = "";
  List<int> ageLevel = [];

  LeafModel(String _id, String chapter, List<int> ageLevel);
}

void main() {
  List<LeafModel> theList = [];
  theList.add(LeafModel("ID", "CHAPTER", [15, 16]));
}

Here's an example of how it'd look.

CodePudding user response:

First create a model where you will hide all your data (be careful with data variables) and then create a list based on your model, then import all your data into your model like this;

List<YourModel> yourModel = mapData.map((doc) => YourModel.fromMap(doc.data())).toList();
  • Related