Instantly share code, notes, and snippets.

Embed
What would you like to do?
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'dart:io';
import 'dart:async';
class Gists{
// instance variable for user
String user;
Future getGistsForUser(String user){
this.user = user;
var url = "https://api.github.com/users/${this.user}/gists";
return http.get(url)
.then((res) => _processResults(res))
.catchError(_handleError);
}
_processResults(http.Response jsonString){
List<String> items = JSON.decode(jsonString.body);
var res = [];
items.forEach((element) => res.add(element["html_url"]));
return res;
}
_handleError(Error error){
print("User ${this.user} not available");
}
}
void main() {
Gists gists = new Gists();
stdout.writeln('Enter your GitHub username');
String username = stdin.readLineSync();
gists.getGistsForUser(username)
.then((res) => print(res.join("\n")))
.catchError((error) => print(error.toString()));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment