1

I have been looking for a way to detect if my Flutter App has access to the internet. I want to use this to let the app make a choice of either sending some data to an API, if there is internet, or saving it to a local database, if there is no internet.

I have tried some of the plugins but all of them seem to detect the network state rather then checking if there is an internet connection.

I have tried just simply sending a ping and checking it that way but when I do that it seems to take a really long time to stop and return the ping failure.

I am also using the DIO library to access the API so I also have tried detecting it through that library but it also takes quite some time to stop and return (around 30 seconds). In this case I think I just hit a timeout but that also isn't really what I am looking for.

Any ideas or better methods of detecting if there is an active internet connection in a rather quick way and not the network state?

  • 1
    Did you try [connectivity](https://pub.dev/packages/connectivity) plugin...? – srikanth7785 Jun 07 '20 at 08:51
  • Try sending it to the API. If that fails, save it locally. Any other strategy comprises trying to foretell the future. – user207421 Jun 07 '20 at 10:17
  • @srikanth7785 Thanks for the tip. I have tried connectivity but the issue I have there is that when I am connected to a Wifi but the Wifi doesn't have an internet connection it still tells me that it is connected. This is normal and in the documentation of the plugin it even states so: "Note that on Android, this does not guarantee connection to Internet. For instance, the app might have wifi access but it might be a VPN or a hotel WiFi with no access." – TheUnriashol Jun 07 '20 at 19:23
  • @MarquisofLorne Ok, seems to be the only way. Going to try it out. Thank you! – TheUnriashol Jun 07 '20 at 19:24
  • It's by no means the only way, but it's the only way that makes sense. – user207421 Jun 08 '20 at 01:01
  • 1
    Does this answer your question? [Check whether there is an Internet connection available on Flutter app](https://stackoverflow.com/questions/49648022/check-whether-there-is-an-internet-connection-available-on-flutter-app) – Rissmon Suresh Jul 22 '21 at 16:44

2 Answers2

1

have a look at the connectivity package Connectivity Package

If the package does not fulfill you requirement, then maybe behind the scene you can try to connect to any website/API using http and check the status

MoAshour
  • 181
  • 2
1

For anybody looking for a similar solution here is what I think that I am going to use.

The Dio library has a http timeout option which seems to work quite well, I was trying to use the Future timeout but that doesn't take into consideration if an http connection was able to get setup.

Here an example from the Dio documentation

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

// Set default configs
dio.options.connectTimeout = 5000; //5s
dio.options.receiveTimeout = 3000;

// or new Dio with a BaseOptions instance.
BaseOptions options = new BaseOptions(
    connectTimeout: 5000,
    receiveTimeout: 3000,
);
Dio dio = new Dio(options);

Thanks for the help. Hope I get it to work the way I want to.