A lightweight json-api oriented network abstraction for Dart and Flutter.
Chlorine is a shortcut for everyone who needs an easy and uniform access to network calls, especially for the ones who want to build a clear and maintenable json api access layer.
Import this package by 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 returns a ChlorineTarget. Dart's cascade notation makes 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 callbacks:
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;
});
});
}
- Fork it!
- Create your feature branch: git checkout -b my-new-feature
- Commit your changes: git commit -am 'Add some feature'
- Push to the branch: git push origin my-new-feature
- Submit a pull request.