0

I am creating a flutter project and I do an HTTP GET request where I get back the following JSON:

[
     {
        "type": "Type one",
        "elements": [{
                "name": "Type one 1 name",
                "code": "TYPEONE_1"
            },
            {
                "name": "Type one 2 name",
                "code": "TYPEONE_2"
            },
            {
                "name": "Type one 3 name",
                "code": "TYPEONE_3"
            }
        ]
    },
    {
        "type": "Type two",
        "elements": [{
                "name": "Type two 1 name",
                "code": "TYPETWO_1"
            },
            {
                "name": "Type two 2 name",
                "code": "TYPETWO_2"
            }
        ]
    }
]

I created these classes:

class Type {
  String type;
  List<Element> elements;
}

class Element {
  String name;
  String code;
}

But from here how to decode the JSON response into a list of Type?

Thanks in advance.

1
  • You can use this site to create classes according to your JSON data, then you can use their constructors to convert your data into objects. Commented Jun 20, 2021 at 1:37

1 Answer 1

1

This model is for your response:


class Model {
    List<Type> el;
}

class Type {
    String type;
    List<Element> elements;
}

class Element {
    String name;
    String code;
}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.