asis 1.0.0 copy "asis: ^1.0.0" to clipboard
asis: ^1.0.0 copied to clipboard

outdated

A small utility library to handle JSON encoded http responses easily, transforms the response into an internal type in the same future chain.

example/main.dart

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));
}
2
likes
0
pub points
0%
popularity

Publisher

unverified uploader

A small utility library to handle JSON encoded http responses easily, transforms the response into an internal type in the same future chain.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter, http

More

Packages that depend on asis