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

outdated

A small utility library to handle JSON encoded HTTP responses, adds couple of useful methods to make it possible to manipulate and transform the data in the same future chain.

ASIS #

A small utility library to handle JSON encoded http responses easily, transforms the response into an internal type of data and adds bunch of functionality to handle data manipulation in the same future chain.

Getting Started #

This package built upon the http package to work with it's internal Response type.

It's also possible to use some of the internal features without depending to http package, such as the extensions, List.as<T>, Future<List<T>>, Future<Iterable<T>>, that helps to manipulate the data in the same future chain.

Example #

Here is a working example that shows the brief functionality of the library, this example highlights how to transform the JSON encoded HTTP body to an internal custom object and a list of that custom object with some data manipulation without leaving the future chain that get's the data from the http call.

import 'package:http/http.dart' as http;
import 'package:asis/asis.dart';

class Task {
  final int userId;
  final int id;
  final String title;
  final bool completed;

  // Task({this.userId, this.id, this.title, this.completed});

  Task.fromJson(Map<String, dynamic> e)
      : userId = e['userId'],
        id = e['id'],
        title = e['title'],
        completed = e['completed'];

  /// Static method to pass the handler directly to the input.
  static Task handler(Map<String, dynamic> e) {
    return Task.fromJson(e);
  }
}

/// Resource url.
var url = 'http://jsonplaceholder.typicode.com/todos';

/// Returns a task object.
Future<Task> task(int id) => http.get('$url/$id').as(Task.handler);

/// Returns a list of tasks.
Future<List<Task>> get tasks => http.get(url).asList(Task.handler);

void main() async {
  /// Get the third task and print the title.
  task(3)
      .then((e) => e.title)
      .then((e) => 'Title of the third task: $e')
      .then(print);

  /// Print the title of the top 3 task.
  tasks.take(3).each((e) => print(e.title));

  /// Count of the completed tasks.
  tasks
      .where((e) => e.completed)
      .fold(0, (p, _) => p + 1)
      .then((e) => 'Number of the completed tasks: $e')
      .then(print);
}
2
likes
0
pub points
0%
popularity

Publisher

unverified uploader

A small utility library to handle JSON encoded HTTP responses, adds couple of useful methods to make it possible to manipulate and transform the data in the same future chain.

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

flutter, http

More

Packages that depend on asis