better_client 0.1.2 better_client: ^0.1.2 copied to clipboard
A simple API client that parse responses for you.
better_client #
Usage #
test.dart #
BetterClient api = BetterClient(
baseUrl: "https://dogs-life-test.herokuapp.com/",
debug: false,
);
Future<List<Dog>> getDogs() async {
ApiResponse data = await api.get(
path: "dogs",
fromJson: (json) => Dog.fromJson(json),
parseList: (body) => List<Dog>.from(body.map((i) => i as Dog)),
);
if (data.success) return data.body;
}
dog_model.dart #
class Dog {
String sId;
String name;
String description;
int energy;
int training;
int friendly;
String image;
String category;
bool isFavorite;
String coat;
String size;
Dog(
{this.sId,
this.name,
this.description,
this.energy,
this.training,
this.friendly,
this.image,
this.category,
this.isFavorite,
this.coat,
this.size});
Dog.fromJson(Map<String, dynamic> json) {
sId = json['_id'];
name = json['name'];
description = json['description'];
energy = json['energy'];
training = json['training'];
friendly = json['friendly'];
image = json['image'];
category = json['category'];
isFavorite = json['isFavorite'];
coat = json['coat'];
size = json['size'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['_id'] = this.sId;
data['name'] = this.name;
data['description'] = this.description;
data['energy'] = this.energy;
data['training'] = this.training;
data['friendly'] = this.friendly;
data['image'] = this.image;
data['category'] = this.category;
data['isFavorite'] = this.isFavorite;
data['coat'] = this.coat;
data['size'] = this.size;
return data;
}
}
Global headers and request headers #
//global headers
BetterClient api = BetterClient(baseUrl: kBaseUrl, debug: false);
api.globalHeaders = {
'Accept': "Application/json",
}
//request headers
ApiResponse response = await api.get(
path: "dogs",
headers: {
'Accept': "Application/json",
}
);
Global debug and request debug #
//global debug
BetterClient api = BetterClient(baseUrl: kBaseUrl, debug: true);
//request debug
ApiResponse response = await api.get(
path: "dogs",
debug: true,
);
Data offset #
if the json coming from the server has a structure like this:
{
status: "success",
data: {
...data
}
}
you may want to parse the content of "data" only. To do this you can proceed as follows:
BetterClient api = BetterClient(baseUrl: kBaseUrl, debug: true);
api.dataOffset = "data";
Getting Started #
This project is a starting point for a Flutter plug-in package, a specialized package that includes platform-specific implementation code for Android and/or iOS.
For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.