Search code examples
jsonfluttermodeltype-conversion

Flutter Models to get a List of string


I have a problem trying to make a special model with this JSON. I actually need to get a list of organization and a list of projects.

I have no problem getting the list of projects since it's an array. But to get the list of Organizations I admit I need help to do something like making a forEach "MyContexts" and save every Organizations in a list and return her. (Maybe this is not the best way)

Here is a example of the JSON :

   {
      "MyContexts": [
        {
          "Organisation": {
            "ID": "xxx",
            "Name": "xxx"
          },
          "Projects": [
            {
              "ID": "xxx",
              "Name": "xxx"
            }
          ]
        }
      ]
    }

To be more precise, I need a list of String because the value will be inserted in a DropdownFormField list of value.

I hope I have made it clear for you to understand, else you can ask me question. Thank you in advance for your help.


Solution

  • You don't need a list of strings, you need a model from your json and list of it's objects. You need a class like this

    class MyContextModel {
      List<MyContexts>? myContexts;
    
      MyContextModel({this.myContexts});
    
      MyContextModel.fromJson(Map<String, dynamic> json) {
        if (json['MyContexts'] != null) {
          myContexts = <MyContexts>[];
          json['MyContexts'].forEach((v) {
            myContexts!.add(new MyContexts.fromJson(v));
          });
        }
      }
    
      Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
        if (this.myContexts != null) {
          data['MyContexts'] = this.myContexts!.map((v) => v.toJson()).toList();
        }
        return data;
      }
    }
    
    class MyContexts {
      Organisation? organisation;
      List<Projects>? projects;
    
      MyContexts({this.organisation, this.projects});
    
      MyContexts.fromJson(Map<String, dynamic> json) {
        organisation = json['Organisation'] != null
            ? new Organisation.fromJson(json['Organisation'])
            : null;
        if (json['Projects'] != null) {
          projects = <Projects>[];
          json['Projects'].forEach((v) {
            projects!.add(new Projects.fromJson(v));
          });
        }
      }
    
      Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
        if (this.organisation != null) {
          data['Organisation'] = this.organisation!.toJson();
        }
        if (this.projects != null) {
          data['Projects'] = this.projects!.map((v) => v.toJson()).toList();
        }
        return data;
      }
    }
    
    class Organisation {
      String? iD;
      String? name;
    
      Organisation({this.iD, this.name});
    
      Organisation.fromJson(Map<String, dynamic> json) {
        iD = json['ID'];
        name = json['Name'];
      }
    
      Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
        data['ID'] = this.iD;
        data['Name'] = this.name;
        return data;
      }
    }
    

    And then you need to make list of it, and you can show it's variables in your DropdownFormField widget like Text(object.Organisation!.name!)