heyr
heyr

Reputation: 5774

How do I asyncronously build an array of data?

  List responseJson;
  List eventDetails = [];

     Future<String> fetchPost() async {
      final response = await http.get(
        "https..",
        headers: {
          HttpHeaders.AUTHORIZATION:
          "Bearer ..."
        });

           for(var i = 0; i< (responseJson?.length ?? 0);i++) {

           final eventDetailsResponse = await http.get(
           "https:.." + responseJson[i]["id"].toString(),
            headers: {
            HttpHeaders.AUTHORIZATION:
            "Bearer .."
          });

      eventDetails.add(json.decode(eventDetailsResponse.body));
    }

this.setState(() {
  responseJson = json.decode(response.body);
  print(eventDetails);
  print(responseJson);

});

Hi there! I have an array defined, I filled the array in with elements, each element is the result of an API call, and then I use the function this.setState to trigger the list view to update, on the first run eventDetails is empty, however on the second run it gets filled in with data, how can I make it await? Thank you in advance.

Upvotes: 0

Views: 57

Answers (1)

Edman
Edman

Reputation: 5605

Are you trying to populate your view from a future?

If that's the case, have a look at FutureBuilder.

Upvotes: 1

Related Questions