Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] android – Is there a way to detect if flutter has access to internet?

Posted on November 11, 2022 By

Solution 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

Solution 2 :

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.

Problem :

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.

READ  [FIXED] java - Element of listview out of bounds(visually) because of round edges
Powered by Inline Related Posts

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

Comments

Comment posted by connectivity

Did you try

Comment posted by user207421

Try sending it to the API. If that fails, save it locally. Any other strategy comprises trying to foretell the future.

Comment posted by TheUnriashol

@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.”

Comment posted by TheUnriashol

@MarquisofLorne Ok, seems to be the only way. Going to try it out. Thank you!

Comment posted by Check whether there is an Internet connection available on Flutter app

Does this answer your question?

Android Tags:android, dio, flutter, networking

Post navigation

Previous Post: [FIXED] Problem with the implementation of the Android Studio Java design libraries
Next Post: [FIXED] android – Kotlin: Typeconverter for other class type

Related Posts

[FIXED] java – Making a password checker in XML (Android Studio) Android
[FIXED] android – Unhandled promise rejection: TypeError: Network request failed expo node backend Android
[FIXED] android – how to parse JSONArray Android
[FIXED] android – error: Type of the parameter must be a class annotated with @Entity or a collection/array of it Android
[FIXED] java – Why Couldn’t I upload Image file using ImageButton in Android Studio? Android
[FIXED] android – ‘by viewModels()’ Kotlin property delegate unresolved reference Android

Archives

  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can Vicks humidifier be used without filter?
  • What color is Spanish green?
  • How old is Jamie in The War That Saved My Life?
  • When should I use scalp massager for hair growth?
  • Can I put polyurethane over liming wax?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme