A lightweight json api network abstraction for flutter.
Chlorine uses async functions and give a easy way to access by binding responses and errors to two different closures.
Import this package adding it in your pubspec.yaml.
...
dependencies:
flutter:
sdk: flutter
chlorine: ">=0.1.0"
...
Create a Target:
import 'package:chlorine/chlorine_target.dart';
class UserTarget {
String baseUrl = 'https://jsonplaceholder.typicode.com/';
getUsers() {
return ChlorineTarget()
..baseUrl = this.baseUrl
..path = 'users'
..method = ChlorineMethod.GET
..headers = Map.from({'Content-Type' : 'application/json'})
//optional closures which will compute te results just before returning the response
..successClosure = (users) {
print(users);
return users;
}
..errorClosure = (error) {
print(error);
return error;
};
}
getUser(int id) {
return ChlorineTarget()
..baseUrl = this.baseUrl
..path = 'users/$id'
..method = ChlorineMethod.GET
..headers = Map.from({'Content-Type' : 'application/json'})
}
}
As you can see a builder methods like getUser() and getUsers() should return a ChlorineTarget. Dart's cascade notation make it very easy to build different instances of the same object.
Create a Chlorine instance and pass to it a builder method from your target class and relative success and error closure:
void main() {
Chlorine chlorine = Chlorine();
chlorine.jsonRequest(UserTarget().getUsers(),
(response) {
print(response.jsonBody);
},
(error) {
print(error.jsonBody);
}
);
chlorine.jsonRequest(UserTarget().getUser(2),
(response) {
print(response.jsonBody);
},
(error) {
print(error.jsonBody);
}
);
}
void main() {
test('getUsers()', () {
Chlorine chlorine = Chlorine();
chlorine.jsonRequest(UserTarget().getUsers(), (response) {
expect(response.statusCode, 200);
}, (error) {
return null;
});
});
}
Add this to your package's pubspec.yaml file:
dependencies:
chlorine: ^0.1.0
You can install packages from the command line:
with Flutter:
$ flutter packages get
Alternatively, your editor might support flutter packages get
.
Check the docs for your editor to learn more.
Now in your Dart code, you can use:
import 'package:chlorine/chlorine.dart';
Version | Uploaded | Documentation | Archive |
---|---|---|---|
0.1.0 | Dec 9, 2018 |
|
|
Popularity:
Describes how popular the package is relative to other packages.
[more]
|
10
|
Health:
Code health derived from static analysis.
[more]
|
99
|
Maintenance:
Reflects how tidy and up-to-date the package is.
[more]
|
70
|
Overall:
Weighted score of the above.
[more]
|
49
|
We analyzed this package on Feb 14, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:
Detected platforms: Flutter
References Flutter, and has no conflicting libraries.
Document public APIs. (-1 points)
67 out of 67 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.
Format lib/chlorine.dart
.
Run flutter format
to format lib/chlorine.dart
.
Format lib/chlorine_target.dart
.
Run flutter format
to format lib/chlorine_target.dart
.
The package description is too short. (-20 points)
Add more detail to the description
field of pubspec.yaml
. Use 60 to 180 characters to describe the package, what it does, and its target use case.
Maintain an example. (-10 points)
Create a short demo in the example/
directory to show how to use this package.
Common filename patterns include main.dart
, example.dart
, and chlorine.dart
. Packages with multiple examples should provide example/README.md
.
For more information see the pub package layout conventions.
Package | Constraint | Resolved | Available |
---|---|---|---|
Direct dependencies | |||
Dart SDK | >=2.0.0 <3.0.0 | ||
flutter | 0.0.0 | ||
http | >=0.12.0 <0.13.0 | 0.12.0+1 | |
Transitive dependencies | |||
async | 2.0.8 | ||
charcode | 1.1.2 | ||
collection | 1.14.11 | ||
http_parser | 3.1.3 | ||
meta | 1.1.6 | 1.1.7 | |
path | 1.6.2 | ||
sky_engine | 0.0.99 | ||
source_span | 1.5.4 | ||
string_scanner | 1.0.4 | ||
term_glyph | 1.1.0 | ||
typed_data | 1.1.6 | ||
vector_math | 2.0.8 | ||
Dev dependencies | |||
flutter_test |