What is connectTimeout parameter in dio flutter

6.5k views Asked by At

I am using DIO package here. My problem is I cannot clearly understand what "connectTimeout" parameter is in dio options. First I thought it is the time that if there is no internet connection, it will try to connect to the API within the specified time.

Docs say the following:

/// Timeout in milliseconds for opening url. /// [Dio] will throw the [DioError] with [DioErrorType.CONNECT_TIMEOUT] type /// when time out.

I tested my assumption by calling the script below with internet disabled

    Dio dio = new Dio();
    Response response;

    dio.options.connectTimeout = 50000;
    dio.options.receiveTimeout = 30000;
    dio.options.sendTimeout = 30000;

    response = await dio
        .get(GeneralStringUtil.someGetAPI);

I instantly got the following as an exception in my log:

DioError [DioErrorType.DEFAULT]: SocketException: Failed host lookup: xyz.com

So my assumption is wrong. What is this "connectTimeout" parameter?

2

There are 2 answers

1
Lalit Rawat On BEST ANSWER

you set a limit of 50000 miliSeconds so dio will trying the http call for 50000 miliSeconds if it will fail to connect to the server during this time period them it will show the exception, the more you increase the time longer the dio will try to connect

0
Yasser Benmman On

I don't think the issue is due to the connectTimeout since in the error message it does not show that timeOut error, but basically in your Dio option the connectTimeOut stand for how many milliseconds Dio will send the HTTP call waiting for the TCP handshake to be established. the receiveTimeout is for how many milliseconds your app will wait to receive the data