asis 1.0.0 asis: ^1.0.0 copied to clipboard
A small utility library to handle JSON encoded http responses easily, transforms the response into an internal type in the same future chain.
import 'package:http/http.dart' as http;
import 'package:asis/asis.dart';
class Todo {
final int userId;
final int id;
final String title;
final bool completed;
// Todo({this.userId, this.id, this.title, this.completed});
Todo.fromJson(dynamic e)
: userId = e['userId'],
id = e['id'],
title = e['title'],
completed = e['completed'];
static Todo handler(dynamic e) => Todo.fromJson(e);
}
var url = 'http://jsonplaceholder.typicode.com/todos';
/// Returns a Todo object.
Future<Todo> todo(int id) => http.get('$url/$id').as(Todo.handler);
/// Returns the list of Todo.
Future<List<Todo>> get todos => http.get(url).asListOf<Todo>(Todo.handler);
void main() async {
/// Get the third todo and print the title..
todo(1).then((value) => print(value.title));
/// Print the title of the top 3 todo.
(await todos).take(3).forEach((todo) => print(todo.title));
}