dioerror [dioerrortype.connectTimeOut]:连接时间熄灭[0ms]

发布于 2025-02-12 08:32:12 字数 1274 浏览 0 评论 0原文

abstract class ApiCalls {
  Future<NoteModel?> createNote(NoteModel value);
  Future<List<NoteModel?>> getAllNotes();
  Future<NoteModel?> updateNote(NoteModel value);
  Future<void> deleteNote(String id);
}

class NoteDB extends ApiCalls {
  final dio = Dio();
  final url = Url();

  @override
  Future<NoteModel?> createNote(NoteModel value) async {
    try {
      final _result = await dio.post<NoteModel>(
        url.baseUrl + url.addNote,
        data: value.toJson(),
      );
      return _result.data;
    } on DioError catch (e) {
      print(e.response?.data);
      print(e);
    } catch (e) {
      print(e.toString());
    }
  }

仍然无法创建。 当Savenote连接到保存图标时,从控制台显示的错误说:

  flutter (30312): null
    I/flutter (30312): DioError [DioErrorType.connectTimeout]: Connecting timed out [0ms]
    I/flutter (30312): Source stack:
    I/flutter (30312): #0      DioMixin.fetch
    package:dio/src/dio_mixin.dart:488
    I/flutter (30312): #1      DioMixin.request
    package:dio/src/dio_mixin.dart:483
    I/flutter (30312): #2      DioMixin.post
    package:dio/src/dio_mixin.dart:97

可以将其送到控制台和API。无效异常被抛出,但在尝试使用和捕获后仍显示出来。 如何解决连接时间的[0ms],或者在哪里应该将持续时间[0]更改为[3000]。

abstract class ApiCalls {
  Future<NoteModel?> createNote(NoteModel value);
  Future<List<NoteModel?>> getAllNotes();
  Future<NoteModel?> updateNote(NoteModel value);
  Future<void> deleteNote(String id);
}

class NoteDB extends ApiCalls {
  final dio = Dio();
  final url = Url();

  @override
  Future<NoteModel?> createNote(NoteModel value) async {
    try {
      final _result = await dio.post<NoteModel>(
        url.baseUrl + url.addNote,
        data: value.toJson(),
      );
      return _result.data;
    } on DioError catch (e) {
      print(e.response?.data);
      print(e);
    } catch (e) {
      print(e.toString());
    }
  }

still not possible to create.
the error shown from the console while saveNote connected to save icon says:

  flutter (30312): null
    I/flutter (30312): DioError [DioErrorType.connectTimeout]: Connecting timed out [0ms]
    I/flutter (30312): Source stack:
    I/flutter (30312): #0      DioMixin.fetch
    package:dio/src/dio_mixin.dart:488
    I/flutter (30312): #1      DioMixin.request
    package:dio/src/dio_mixin.dart:483
    I/flutter (30312): #2      DioMixin.post
    package:dio/src/dio_mixin.dart:97

can,t get it to the console and api. null exception is thrown but after try and catch used but still showing.
how can i solve Connecting timed out [0ms]or where should i change the time duration [0] to [3000].

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七颜 2025-02-19 08:32:12

您可以在dio.options中使用以下属性将其设置为默认配置。 来自文档:

var dio = Dio(); // with default Options

// Set default configs
dio.options.baseUrl = 'https://www.xx.com/api';
dio.options.connectTimeout = 5000; //5s
dio.options.receiveTimeout = 3000;

// or new Dio with a BaseOptions instance.
var options = BaseOptions(
  baseUrl: 'https://www.xx.com/api',
  connectTimeout: 5000,
  receiveTimeout: 3000,
);
Dio dio = Dio(options);

或您可以<代码>发布带有请求选项参数设置ConnectTimeOut和/或codeivetimeout来自文档

   /// Timeout in milliseconds for opening url.
  int connectTimeout;

  ///  Whenever more than [receiveTimeout] (in milliseconds) passes between two events from response stream,
  ///  [Dio] will throw the [DioError] with [DioErrorType.RECEIVE_TIMEOUT].
  ///  Note: This is not the receiving time limitation.
  int receiveTimeout;

You can use the following properties in dio.options to set it the default configurations. From the docs:

var dio = Dio(); // with default Options

// Set default configs
dio.options.baseUrl = 'https://www.xx.com/api';
dio.options.connectTimeout = 5000; //5s
dio.options.receiveTimeout = 3000;

// or new Dio with a BaseOptions instance.
var options = BaseOptions(
  baseUrl: 'https://www.xx.com/api',
  connectTimeout: 5000,
  receiveTimeout: 3000,
);
Dio dio = Dio(options);

Or you can post with the request options parameter setting connectTimeout and/or receiveTimeout. From the docs:

   /// Timeout in milliseconds for opening url.
  int connectTimeout;

  ///  Whenever more than [receiveTimeout] (in milliseconds) passes between two events from response stream,
  ///  [Dio] will throw the [DioError] with [DioErrorType.RECEIVE_TIMEOUT].
  ///  Note: This is not the receiving time limitation.
  int receiveTimeout;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文