Marcos Placona
Marcos Placona
Marcos an experienced Developer Relations leader turned solopreneur, GDE and in ❤️ with Open Source
1 min read

Categories

  • Dart

I wrote a little article about how to retrieve gists from GitHub, and +Seth Ladd quite rightly pointed something out.

Seth Ladd makes a good point

Let me elaborate a little bit on what he means.

getGistsForUser(String user){
  this.user = user;
  var url = "https://api.github.com/users/${this.user}/gists";
  http.get(url)
    .then(_processResults)
    .catchError(_handleError);
}

In my sample code (cut down for brevity, but you can still check it out here), I am indeed using Futures, so the thread is not locked to this request, and my code could be happily doing other things. However, I’m not informing the client about this future, or allowing it to make use of it.

So when I make a request to it, I’m simply doing:

gists.getGistsForUser(username);

Which works in this case as my getGistsForUser method is dealing with displaying the gists itself; therefore the client isn’t waiting as the method returns void.

If I then wanted to get my client know exactly when the method had returned, I would need to make changes to my code by simply making the method getGistsForUser return a Future. This is what the code would look like after making a few changes:

As you can see, I’ve only updated a few things, but now my response is handled by my client.

To simplify, I have written a much simpler example that only handles an HTTP request, without prompting the user for any information.

And this is how you go about returning futures from HTTP requests

YOU MIGHT ALSO LIKE