asis 1.1.3

  • Readme
  • Changelog
  • Example
  • Installing
  • 59

ASIS #

A small utility library to handle JSON encoded http responses easily, transforms the response into an internal type of data and extends the capabilities of Response, Iterable, List, Future<Response>, Future<Iterable>, Future<List> to handle data manipulation on 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, Future<List<T>>, Future<Iterable<T>>, that helps to manipulate the data on 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 on top of some data manipulation without leaving the same 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);
}

[1.1.3] Iterable extension #

  • Adds as<T>() to [Iterable]
  • Adds asList<T>() to [Iterable]

[1.1.2] Automation #

  • Adds GitHub action for Dart CI.

[1.1.1] Initial release #

  • Adds as<T>() to [List]
  • Adds jsonBody to [http.Response]
  • Adds as<T>() to [http.Response]
  • Adds asList<T>() to [http.Response]
  • Adds Future<Iterable<T>>
  • Adds Future<List<T>>
  • Adds Future<Response<T>>

example/main.dart

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(1)
      .then((e) => e.title)
      .then((e) => 'Title of the first 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);
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  asis: ^1.1.3

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:asis/asis.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
18
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
59
Learn more about scoring.

We analyzed this package on Jul 11, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.15

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
http ^0.12.1 0.12.1
Transitive dependencies
charcode 1.1.3
collection 1.14.13 1.15.0-nullsafety
http_parser 3.1.4
meta 1.2.2 1.3.0-nullsafety
path 1.7.0
pedantic 1.9.2
source_span 1.7.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.2.0 1.3.0-nullsafety
Dev dependencies
test ^1.14.6