Last updated:
0 purchases
otter
Otter #
Flutter plugin designed to simplify the management of HTTP requests in offline mode :
Cache HTTP requests in the event of network unavailability.
Monitor network connectivity status continuously.
Process and dispatch cached HTTP requests upon restoration of network access.
Installation #
dependencies:
otter: ^1.0.3
copied to clipboard
Configuration #
Here's a simple configuration.
⚠️ Note that you can create your own OTTaskHandler and pass it as a parameter to the init() function. Example
import 'package:otter/handler/task_handler.dart';
import 'package:otter/database/database_provider.dart';
import 'package:otter/network/network_helper.dart';
void main() async {
runApp(const App());
Otter.init(OTTaskHandler());
}
copied to clipboard
Using #
⚠️ Note that you can create your own OTTask and pass it as a parameter to the addOneTask() or addMultipleTasks() functions. Example
addOneTask #
GET
import 'package:otter/network/http_method.dart';
import 'package:otter/database/database_store.dart';
import 'package:otter/model/task.dart';
final store = OTDBStore();
final task = OTTask(Uri.https('example.com', '/path/example'), HTTPMethod.get, {}, {});
store.addOneTask(
task,
didFail: (task, error, stackTrace) => {
// Handle error saving here
},
networkAvailable: () => {
// Perform HTTP request here, if network is available
},
);
copied to clipboard
POST
import 'package:otter/network/http_method.dart';
import 'package:otter/database/database_store.dart';
import 'package:otter/model/task.dart';
final store = OTDBStore();
final task = OTTask(Uri.https('example.com', '/path/example'),HTTPMethod.post,
{'Content-Type': 'application/json'}, {'key': 'value'});
store.addOneTask(
task,
didFail: (task, error, stackTrace) => {
// Handle error saving here
},
networkAvailable: () => {
// Perform HTTP request here, if network is available
},
);
copied to clipboard
PATCH
import 'package:otter/network/http_method.dart';
import 'package:otter/database/database_store.dart';
import 'package:otter/model/task.dart';
final store = OTDBStore();
final task = OTTask(Uri.https('example.com', '/path/example'), HTTPMethod.patch,
{'Content-Type': 'application/json'}, {'key': 'value'});
store.addOneTask(
task,
didFail: (task, error, stackTrace) => {
// Handle error saving here
},
networkAvailable: () => {
// Perform HTTP request here, if network is available
},
);
copied to clipboard
PUT
import 'package:otter/network/http_method.dart';
import 'package:otter/database/database_store.dart';
import 'package:otter/model/task.dart';
final store = OTDBStore();
final task = OTTask(Uri.https('example.com', '/path/example'), HTTPMethod.put,
{'Content-Type': 'application/json'}, {'key': 'value'});
store.addOneTask(
task,
didFail: (task, error, stackTrace) => {
// Handle error saving here
},
networkAvailable: () => {
// Perform HTTP request here, if network is available
},
);
copied to clipboard
DELETE
import 'package:otter/network/http_method.dart';
import 'package:otter/database/database_store.dart';
import 'package:otter/model/task.dart';
final store = OTDBStore();
final task = OTTask(Uri.https('example.com', '/path/example'), HTTPMethod.delete, {}, {});
store.addOneTask(
task,
didFail: (task, error, stackTrace) => {
// Handle error saving here
},
networkAvailable: () => {
// Perform HTTP request here, if network is available
},
);
copied to clipboard
addMultipleTasks #
import 'package:otter/network/http_method.dart';
import 'package:otter/database/database_store.dart';
import 'package:otter/model/task.dart';
final store = OTDBStore();
final getTask = OTTask(Uri.https('example.com', '/path/example'), HTTPMethod.get, {}, {});
final postTask = OTTask(Uri.https('example.com', '/path/example'),HTTPMethod.post,
{'Content-Type': 'application/json'}, {'key': 'value'});
final deleteTask = OTTask(Uri.https('example.com', '/path/example'), HTTPMethod.delete, {}, {});
store.addMultipleTasks(
[getTask, postTask, deleteTask],
didFail: (task, error, stackTrace) => {
// Handle error saving here
},
networkAvailable: () => {
// Perform HTTP request here, if network is available
},
);
copied to clipboard
Custom OTTaskHandler #
TaskHandler is an important concept in Otter.
It is in this class that the post-processing of HTTP requests is managed.
import 'package:otter/handler/task_handler.dart';
import 'package:otter/model/task.dart';
class ExampleTaskHandler extends OTTaskHandler {
@override
void didFail(OTTask task, Object? error, StackTrace stackTrace) {
// Handle task failure
}
@override
Future<void> didFinish(OTTask task) async {
await super.didFinish(task);
// Complete processing of a given task
}
@override
void didSuccess(OTTask task, String response) {
// Handle the successful completion of a task
}
}
copied to clipboard
Custom OTTask #
Task is a representation of HTTP request
import 'package:otter/model/task.dart';
class ExampleTask extends OTTask {
ExampleTask(super.uri, super.method, super.headers, super.body);
}
copied to clipboard
[email protected]
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.