better_client 0.1.5
better_client: ^0.1.5 copied to clipboard

A simple http client that speeds up error handling and facilitates parsing of the json returned by the server.

better_client #

A simple http client that speeds up error handling and facilitates parsing of the json returned by the server

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,
);

Json parsing #

If the server returns an object, automatic parsing can be done if the model class implements the fromJson method:

ApiResponse data = await api.get(
    path: "dog/3",
    fromJson: (json) => Dog.fromJson(json),
);

If the server returns an array, you have to add the parseList param:

ApiResponse data = await api.get(
    path: "dogs/",
    fromJson: (json) => Dog.fromJson(json),
    parseList: (body) => List<Dog>.from(body.map((i) => i as Dog)),
);

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.

2
likes
80
pub points
5%
popularity

A simple http client that speeds up error handling and facilitates parsing of the json returned by the server.

Homepage

Documentation

API reference

License

BSD-3-Clause (LICENSE)

Dependencies

flutter, http

More

Packages that depend on better_client