flutter-tutorials icon indicating copy to clipboard operation
flutter-tutorials copied to clipboard

How to handle "Network Aware" for web too along with mobile?

Open pallavbohara opened this issue 5 years ago • 6 comments

Hello FilledStacks Team, I really love your tutorials. While following tutorial for network connectivity I found that connectivity plugin is not returning correct values for web I also tried to add connectivity_for_web. Although web is offline always. But I cannot setup for both in single code base. Please guide to do so.

Regards, Pallav Bohara

pallavbohara avatar Aug 28 '20 09:08 pallavbohara

Hi there,

In my experience there won't be much difference. what exactly are you struggling with setting up for both?

FilledStacks avatar Aug 30 '20 06:08 FilledStacks

Hello, Thanks for quick reply. Actually right now I am using connectivity: ^0.4.9+2 and it is returning "ConnectivityStatus.Offline" every time for web(which is I think wrong). And I think if we can check some where if it is web then just avoid connectivity check as web always will have connectivity.

pallavbohara avatar Aug 31 '20 13:08 pallavbohara

@pallavbohara sounds like an option. the constant kIsWeb tells you if it's running on web or not. You can use that for your logic.

FilledStacks avatar Aug 31 '20 15:08 FilledStacks

Hello, It is not working. I am trying like this(In reference to your 'NetWork Aware Code' in connectivity_servie.dart :

ConnectivityStatus _getStatusFromResult(ConnectivityResult result) { if (kIsWeb) { return ConnectivityStatus.Cellular; } switch (result) { case ConnectivityResult.mobile: return ConnectivityStatus.Cellular; break; case ConnectivityResult.wifi: return ConnectivityStatus.Wifi; break; case ConnectivityResult.none: return ConnectivityStatus.Offline; break; default: return ConnectivityStatus.Offline; } }

But It is always picking Offline code. I am attacing whole project zip in case you need.

network_sensitive (2).zip

pallavbohara avatar Sep 01 '20 07:09 pallavbohara

Hello, It is not working. I am trying like this(In reference to your 'NetWork Aware Code' in connectivity_servie.dart :

ConnectivityStatus _getStatusFromResult(ConnectivityResult result) { if (kIsWeb) { return ConnectivityStatus.Cellular; } switch (result) { case ConnectivityResult.mobile: return ConnectivityStatus.Cellular; break; case ConnectivityResult.wifi: return ConnectivityStatus.Wifi; break; case ConnectivityResult.none: return ConnectivityStatus.Offline; break; default: return ConnectivityStatus.Offline; } }

But It is always picking Offline code. I am attacing whole project zip in case you need.

network_sensitive (2).zip

Sorry, I have mistakenly created a new issue.

pallavbohara avatar Sep 01 '20 09:09 pallavbohara

That's the constant I used, maybe if changed. there's other ways to check for web as well. I don't have it on hand right now.

FilledStacks avatar Sep 02 '20 06:09 FilledStacks