DioError [DioErrorType.connectTimeout]: Connecting timed out [0ms]

Viewed 499
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].

1 Answers

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;
Related