Dio: How to check condition for `No Internet Connection'

Created on 15 Jun 2020  ·  5Comments  ·  Source: flutterchina/dio

New Issue Checklist

  • [x] I have searched for a similar issue in the project and found none

Issue Description and Steps

How to check condition for No Internet Connection using DioErrorType?
The test case, when I turn off the wifi and mobile data.

Code

try {
      yield MoviesLoading();
      var movies = await repository.getNowPlaying(ApiConstant.apiKey, ApiConstant.language);
      if (movies.results.isEmpty) {
        yield MoviesNoData(AppConstant.noData);
      } else {
        yield MoviesHasData(movies);
      }
    } on DioError catch (e) {
      if (e.type == DioErrorType.CONNECT_TIMEOUT || e.type == DioErrorType.RECEIVE_TIMEOUT) {
        yield MoviesNoInternetConnection(AppConstant.noInternetConnection);
      } else {
        yield MoviesError(e.toString());
      }
    }

e.type == DioErrorType.CONNECT_TIMEOUT || e.type == DioErrorType.RECEIVE_TIMEOUT for handle Timeout, yield MoviesError(e.toString()); for handle Error from Dio. So how to handle No Internet Connection using DioError?

stale

Most helpful comment

How is this automatically closed?
This is a major issue, especially for file uploads.

No connection results in the most unwanted behaviour possible:

  • Dio "uploads" the entire file and actually updates the progress as if it is being uploaded (weird using UDP-style with no checks here, never ever seen that anywhere)
  • Dio does not detect that there is in fact no connection
  • Dio only reacts after the set overall timeout with an error

This behaviour is basically valid for any request, it just throws an error faster with smaller payload or GET requests.

To me this makes this package unusable outside the most simplest requests.
And using more plugins to wrap this one with correct functionality should not be the goal.

Dio simply does not implement the http correctly. There are e.g. options/pre-flight requests for exactly this reason.

All 5 comments

True this is the major problem dio is having, We can simply integrate Dio with connectivity package and before sending a request we can check for connection, if connection doesn't exists, then we can add a new DioError called NotConnectedException, which will give us the liberty to use it efficiently.

@jaydangar I don't recommend using connectivity package because If you are connected to wifi or mobile data but actually there is no internet data. The connectivity return true (has internet data). So I will not recommend it to check the internet connection. Because it's just to check the status not to check the internet connection.

There's another package called data_connection_checker which involved the exact thing we want.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, please make sure it is up to date and if so, add a comment that this is still an issue to keep it open. Thank you for your contributions.

How is this automatically closed?
This is a major issue, especially for file uploads.

No connection results in the most unwanted behaviour possible:

  • Dio "uploads" the entire file and actually updates the progress as if it is being uploaded (weird using UDP-style with no checks here, never ever seen that anywhere)
  • Dio does not detect that there is in fact no connection
  • Dio only reacts after the set overall timeout with an error

This behaviour is basically valid for any request, it just throws an error faster with smaller payload or GET requests.

To me this makes this package unusable outside the most simplest requests.
And using more plugins to wrap this one with correct functionality should not be the goal.

Dio simply does not implement the http correctly. There are e.g. options/pre-flight requests for exactly this reason.

Was this page helpful?
0 / 5 - 0 ratings