Skip to content

Instantly share code, notes, and snippets.

@soonsam123
Created September 22, 2019 17:43
Show Gist options
  • Save soonsam123/8f8f42c16d5b01340501312fba57287f to your computer and use it in GitHub Desktop.
Save soonsam123/8f8f42c16d5b01340501312fba57287f to your computer and use it in GitHub Desktop.
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:bloc_pattern/models/user.dart';
class UsersApiClient {
static const baseUrl = 'http://api.github.com';
final http.Client httpClient;
UsersApiClient({this.httpClient})
: assert(httpClient != null);
Future<List<User>> fetchUsers() async {
final List<User> allUsers = [];
http.Response response = await httpClient.get('$baseUrl/users');
List<dynamic> responseData = jsonDecode(response.body);
responseData.forEach((singleUser) {
allUsers.add(User(login: singleUser['login'], id: singleUser['id']));
});
return allUsers;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment