stash_dio 3.0.0-dev.1 stash_dio: ^3.0.0-dev.1 copied to clipboard
Dio extension for the stash caching API. Provides an interceptor that uses Stash as a caching layer for Dio
stash_dio #
A stash Dio extension
Overview #
This integration of stash with dio provides a caching interceptor that is able to return the response from a Cache instead of hitting the backend system.
Getting Started #
Add this to your pubspec.yaml
(or create it) replacing x.x.x with the latest version of stash_dio:
dependencies:
stash_dio: ^x.x.x
Run the following command to install dependencies:
dart pub get
Finally, to start developing import the library:
import 'package:stash_dio/stash_dio.dart';
Usage #
import 'package:dio/dio.dart';
import 'package:stash_dio/dio_stash.dart';
class Task {
final int id;
final String title;
final bool completed;
Task({this.id, this.title, this.completed = false});
/// Creates a [Task] from json map
factory Task.fromJson(Map<String, dynamic> json) => Task(
id: json['id'] as int,
title: json['title'] as String,
completed: json['completed'] as bool);
/// Creates a json map from a [Task]
Map<String, dynamic> toJson() =>
<String, dynamic>{'id': id, 'title': title, 'completed': completed};
@override
String toString() {
return 'Task ${id}: "${title}" is ${completed ? "completed" : "not completed"}';
}
}
void main() async {
// Configures a a dio client
final dio = Dio(BaseOptions(baseUrl: 'https://jsonplaceholder.typicode.com'))
..interceptors.addAll([
newMemoryCacheInterceptor('/todos/1', 'task'),
LogInterceptor(
requestHeader: false,
requestBody: false,
responseHeader: false,
responseBody: false)
]);
// First call, executes the request and response is received
final task1 = await dio
.get('/todos/1')
.then((Response<dynamic> response) => Task.fromJson(response.data));
print(task1);
// Second call, executes the request and the response is received from the
// cache
final task2 = await dio
.get('/todos/1')
.then((Response<dynamic> response) => Task.fromJson(response.data));
print(task2);
}
Features and Bugs #
Please file feature requests and bugs at the issue tracker.
License #
This project is licensed under the MIT License - see the LICENSE file for details