0

I have a problem, how to make list contains "logo" element from JSON and the result will be like this?

List<String> logo = [
'assets/images/event/482dd2272b83506e0e3c70cb86b65ec1.jpg',
'assets/images/event/3caf5cf0f593dae4a686b765c8db02f3.jpg',
'assets/images/event/3caf5cf0f593dae4a686b765c8db02f3.jpg',
];

and the JSON code below:

[
    {
        "id": "278",
        "nama": "KFC Super Deal",
        "tglawal": "07 Sep 2020",
        "tglakhir": "13 Sep 2020",
        "jenis": "Promo",
        "tenant": "KFC",
        "logo": "assets/images/event/482dd2272b83506e0e3c70cb86b65ec1.jpg"
    },
    {
        "id": "277",
        "nama": "Cash Back 150K",
        "tglawal": "20 Aug 2020",
        "tglakhir": "30 Aug 2020",
        "jenis": "Promo",
        "tenant": "SPORT STATION",
        "logo": "assets/images/event/3caf5cf0f593dae4a686b765c8db02f3.jpg"
    },
    {
        "id": "276",
        "nama": "Cash Back 150K",
        "tglawal": "20 Aug 2020",
        "tglakhir": "30 Aug 2020",
        "jenis": "Promo",
        "tenant": "SKECHERS",
        "logo": "assets/images/event/5138cbf11662ad982828209376b71ddd.jpg"
    },
]

Anyone know how to solve my problem? Thank you :)

1
  • Welcome to Stack Overflow. Please read how to ask good questions. Make sure your question covers these 3 elements: 1. Problem Statement 2. Your Code (it should be Minimal, Reproducible Example 3. Error Message (preferably full Traceback to help others review and provide feedback). While I see the problem statement, I don't see your code. Can you add your code. Commented Oct 7, 2020 at 4:37

2 Answers 2

1

You can do like this

List<String> logo = new List<String>();
  
  var response = [
    {
        "id": "278",
        "nama": "KFC Super Deal",
        "tglawal": "07 Sep 2020",
        "tglakhir": "13 Sep 2020",
        "jenis": "Promo",
        "tenant": "KFC",
        "logo": "assets/images/event/482dd2272b83506e0e3c70cb86b65ec1.jpg"
    },
    {
        "id": "277",
        "nama": "Cash Back 150K",
        "tglawal": "20 Aug 2020",
        "tglakhir": "30 Aug 2020",
        "jenis": "Promo",
        "tenant": "SPORT STATION",
        "logo": "assets/images/event/3caf5cf0f593dae4a686b765c8db02f3.jpg"
    },
    {
        "id": "276",
        "nama": "Cash Back 150K",
        "tglawal": "20 Aug 2020",
        "tglakhir": "30 Aug 2020",
        "jenis": "Promo",
        "tenant": "SKECHERS",
        "logo": "assets/images/event/5138cbf11662ad982828209376b71ddd.jpg"
    },
];
  
  for(int i=0; i < response.length; i++){
    logo.add(response[i]['logo']);
  }
  print(logo);

then the output will be like this

['assets/images/event/482dd2272b83506e0e3c70cb86b65ec1.jpg',
'assets/images/event/3caf5cf0f593dae4a686b765c8db02f3.jpg',
'assets/images/event/3caf5cf0f593dae4a686b765c8db02f3.jpg',]
Sign up to request clarification or add additional context in comments.

Comments

0

The simplest way is to use for loop and spread operator:

  final logos = [
    for (var element in json) ...{
      element['logo'],
    }
  ];

  print(logos);

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.