1

我的应用程序我正在使用DioRetrofit子库,在其中我不确定如何为此定义缓存机制,

Retrofit我在示例代码中找到了这一行:

@GET("")
Future<String> testCustomOptions(@DioOptions() Options options);

现在我如何在这个http请求上定义缓存?

改造文档:

void main(List<String> args) {
  final dio = Dio();   // Provide a dio instance
  dio.options.headers["Demo-Header"] = "demo header";   // config your dio headers globally
  final client = RestClient(dio);
  
  client.getTasks().then((it) => logger.i(it));

dio-http-cache文档:

QuickStart

Add a dio-http-cache interceptor in Dio :

dio.interceptors.add(DioCacheManager(CacheConfig(baseUrl: "http://www.google.com")).interceptor);

Set maxAge for a request :

Dio().get(
  "http://www.google.com",
  options: buildCacheOptions(Duration(days: 7)),
);

我的完整实现代码:

Provider(
   create: (_) => MyApis.create(),
),


abstract class MyApis{
  factory MyApis(Dio dio, {String baseUrl}) = _MyApis;

  @GET("/login")
  Future<HttpResponse<PageInformation>> login(@DioOptions() Options options);

  static MyApis create() {
    final dio = Dio();
    dio.options.headers['Content-Type'] = 'application/json';
    dio.options.receiveTimeout = 60000;
    dio.options.connectTimeout = 120000;
    return _MyApis(dio);
  }
}
4

1 回答 1

2

在 API 添加点调用中添加参数

Future<HttpResponse<PageInformation>> login(@DioOptions() Options options);

在 Dio 中添加拦截器

dio.interceptors.add(DioCacheManager(CacheConfig(baseUrl: 'YOUR BASE URL')).interceptor);

为 Endpoint 参数创建一个对象

Options options = buildCacheOptions(Duration(days: 10),forceRefresh: true);

调用您的 API 端点

myapi.login(options)
 
于 2020-10-08T16:08:43.760 回答