Home > Blockchain >  Flutter Models to get a List of string
Flutter Models to get a List of string

Time:08-09

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.

CodePudding user response:

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!)

CodePudding user response:

I may be understanding it in the wrong way, but I am thinking the JSON contains a map, which values are {"Organisation": ..., "Projects": ...}. If that's not the case, you can stop reading and correct me now.

Let's say json is the variable containing the JSON structure of your example.

Then you can do

final orgs = json.values.map((context) => context['Organisation']).toList();
final projs = json.values.map((context) => context['Projects']).toList();

Or you can use forEach like you mentioned, which requires slightly more coding, but that's not much other than a style of preference. If you think a particular approach is more straightforward and more understandable, go for it.

A side note: usually we use for loops explicit instead of forEach.

  • Related