dio_sync 1.0.1 dio_sync: ^1.0.1 copied to clipboard
add sync mechaniscm to dio requests
import 'dart:async';
import 'dart:math';
import 'package:dio/dio.dart';
import 'package:dio_sync/dio_sync.dart';
import 'package:flutter/material.dart';
import 'package:hive_flutter/hive_flutter.dart';
void main() async {
await Hive.initFlutter();
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
late final Dio dio;
LazySyncBox? box;
StreamSubscription? subscription;
Iterable<BoxValue> events = [];
@override
void initState() {
dio = Dio(BaseOptions(baseUrl: 'https://jsonplaceholder.typicode.com'));
dio.interceptors.add(_Interceptor());
LazySyncBox.create(client: dio, automaticSyncType: AutomaticSyncType.single)
.then((value) {
setState(() {
box = value;
events = box?.values ?? [];
});
subscription = box?.listen(_onValuesChanged);
});
super.initState();
}
void addRequest() {
dio.get('/todos/1');
}
@override
void dispose() {
subscription?.cancel();
box?.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: ListView(
children: events.map(_buildItem).toList(),
),
floatingActionButton: FloatingActionButton(
onPressed: addRequest,
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
FutureBuilder<BoxObject?> _buildItem(BoxValue e) {
return FutureBuilder(
future: e.value,
builder: (BuildContext context, AsyncSnapshot<BoxObject?> snapshot) {
var value = snapshot.data;
return ListTile(
title: Text("${value?.path}(${value?.uuid})"),
subtitle: Text(value?.exception?.message ?? ""),
trailing: Text(value?.status.name ?? ""),
);
},
);
}
void _onValuesChanged(BoxEvent event) {
setState(() {
events = box?.values ?? [];
});
}
}
class _Interceptor implements Interceptor {
@override
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
print("Requesting: ${options.path} extra: ${options.extra}");
handler.next(options);
}
@override
void onError(DioException err, ErrorInterceptorHandler handler) {
print(
"Error: ${err.requestOptions.path} extra: ${err.requestOptions.extra}");
handler.next(err);
}
@override
void onResponse(Response response, ResponseInterceptorHandler handler) {
print(
"Error: ${response.requestOptions.path} extra: ${response.requestOptions.extra}");
handler.next(response);
}
}