1

I have a complex API which has a json array. i want to display the details in the JSON in a flutter listView. The following is my json

{
    "hours": [
        {
            "time": "2021-03-23T00:00:00+00:00",
            "waveHeight": {
                "icon": 1.35,
                "meteo": 1.25,
                "noaa": 1.28,
                "sg": 1.25
            }
        },
{
            "time": "2021-03-23T00:00:00+00:00",
            "waveHeight": {
                "icon": 1.35,
                "meteo": 1.25,
                "noaa": 1.28,
                "sg": 1.25
            }
        },
    ],
}

This is the fetch data function

void getJsonData() async {
    String url2 =
        'https://api.stormglass.io/v2/weather/point?lat=5.9774&lng=80.4288&params=waveHeight&start=2021-03-23&end2021-03-24';
    
    String apiKey =
        'sxascdsvfdyhujn5787654gb-7a54-11eb-8302-0242ac130002';
    print('0');

    try {
      Response response = await get(Uri.parse(url2),
          headers: {HttpHeaders.authorizationHeader: apiKey});

      var jsonData = jsonDecode(response.body);
      List data = jsonData["hours"];

      data.forEach((element) {
        Map obj = element;
        Map wave = obj['waveHeight'];
        String time = obj['time'];

        print(time);
        double icon = wave['icon'];

        print(icon);
      });
    } catch (e) {
      print(e);
    }
  }

All the JSON data are successfully fetched and displayed in the console. But I want to display the data in a flutter ListView. How can i do that?

1 Answer 1

1

firstly define a new class

class Wave {
  final DateTime time;
  final double icon;
  final double meteo;
  final double noaa;
  final double sg;

  Wave ({
    this.time,
    this.icon,
    this.meteo,
    this.noaa,
    this.sg,
  });
}

create an empty list

List<Wave> _data = [];

Edit your code with the below code

      final _extractedData = json.decode(result.body) as Map<String, dynamic>;

      List<Wave> _fetchedData = [];

      _extractedData['hours'].forEach((value) {
        _fetchedData.add(Wave(
          time: value['time'],
          icon: value['icon'],
          meteo: value['meteo'],
          noaa: value['noaa'],
          sg: value['sg'],
        ));
      });
      _data = _fetchedData;
 

_data has the new list of data received from json.

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

3 Comments

@Learnado if this code has helped you, then please give it a tick.
It is working but when I print the _data it shows as Instance of 'Wave'
obviously it will show that as it is Instance of Wave. _data is a list of type Wave. to use it, try _data[index].time. If you are not familiar with coding, please try using help of some course. Flutter is an easy language to learn. But if you don't have a coding background or you are new in it, it will get very difficult.

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.