What is best way to handle no internet connection in Flutter

Viewed 1041

There are several ways to handle no internet connection exception. But they didn't fit at all. That is all I get from my experience but not good at all.

  1. Handling exception handling when an error occurs
  • When handling SocketException, it caches all networking exception even it's about HTTP error or no internet connection.
  1. Using Connectivity
  • It only assumes wifi or mobile data is enabled or disabled.
  1. Mixing Connectivity with pinging or data connection checker package
  • This was a good idea but when I release my app lot's of users complain about my app says "no internet connection even though they have internet"
  1. Pinging to google
  • If users from google banned country for example in China. This approach will become useless.

So I think we need a good design pattern or something on that problem. What is the proper way to handle this error?

1 Answers

One way is to just be sure that the internet connection is available - make such check at startup. Without pinging anything. I was a bit frustrated with this myself and wrote a really simple plugin that utilises some Java code to just check if the Internet is available on device (obviously, this is for Android only, gotta add iOS version later).

Feel free to check it out: https://pub.dev/packages/check_inet

Related