Ошибка: значение типа ‘RequestOptions’ не может быть присвоено переменной типа ‘Future’

#flutter

#flutter

Вопрос:

Я получил ошибку ниже при компиляции проекта flutter, загруженного с github.

вот код:

 import 'package:dio/dio.dart';

class HeaderInterceptors extends InterceptorsWrapper {
  @override
  onRequest(RequestOptions options) {
    // 超时
    options.connectTimeout = 15000;
    return options;
  }
}
 

и вот ошибка :

 lib/common/service/interceptors/header_interceptor.dart:8:12: Error: A value of type 'RequestOptions' can't be assigned to a variable of type 'Future<dynamic>'.
 - 'RequestOptions' is from 'package:dio/src/options.dart' ('/D:/intellij/flutter_windows_1.22.5-stable/flutter/.pub-cache/hosted/pub.flutter-io.cn/dio-3.0.10/lib/src/options.dart').
 - 'Future' is from 'dart:async'.
    return options;
           ^
 

есть идеи?

Комментарии:

1. Преобразуйте его в onRequest(параметры RequestOptions) async { … }. Поскольку возвращаемое значение является динамическим, оно должно иметь возможность возвращать RequestOptions

Ответ №1:

InterceptorsWrapper.onRequest() является пустым. Для изменения connectionTimeout вы можете вызвать RequestInterceptorHandler.next(RequestOptions) обновление конфигурации.

 @override
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
  options.connectTimeout = 15000;
  handler.next(options);
}