Skip to content

A Dart library to facilitate API Rest service consumption, without the need for automatic code generation or reflection it does the JSON mapping for the Entity, besides allowing to keep the data in CACHE using Local Storage for fast loading and server load reduction

License

Notifications You must be signed in to change notification settings

insinfo/essential_rest

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

essential_rest is an API to consume REST web services, it does the JSON mapping for the Entity, besides allowing to keep the data in CACHE using Local Storage for fast loading and load reduction to the Server

Usage

A simple usage example:

import 'package:essential_rest/essential_rest.dart';
import 'package:essential_rest/src/response_list.dart';

//entity model definition
class ExampleModel {
  int id;
  int userId;
  String title;
  bool completed;

  ExampleModel({this.userId, this.id, this.title, this.completed});

  ExampleModel.fromMap(Map<String, dynamic> json) {
    userId = json['userId'];
    id = json['id'];
    title = json['title'];
    completed = json['completed'];
  }

  @override
  Map<String, dynamic> toMap() {
    final data = Map<String, dynamic>();
    data['userId'] = userId;
    data['id'] = id;
    data['title'] = title;
    data['completed'] = completed;
    return data;
  }
}

void main() async {
  var rest = RestClientGeneric<ExampleModel>(factory: {ExampleModel: (x) => ExampleModel.fromMap(x)});
  rest.protocol = ProtocolType.https;
  rest.host = 'jsonplaceholder.typicode.com';

  //get list of ExampleModel
  var resp = await rest.getAll('/todos');
  RList<ExampleModel> list = resp.resultListT;
  list.forEach((item) {
    print('list item: ${item.title}');
  });

  //get single ExampleModel
  resp = await rest.get('/todos/1');
  var item = resp.resultT;
  print('single item: ${item.title}');

  //create new item
  resp = await rest.post('/todos', body: ExampleModel(title: 'test').toMap());
  print('create item: ${resp.status}');
}

About

A Dart library to facilitate API Rest service consumption, without the need for automatic code generation or reflection it does the JSON mapping for the Entity, besides allowing to keep the data in CACHE using Local Storage for fast loading and server load reduction

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages