Skip to content
This repository was archived by the owner on Jan 28, 2025. It is now read-only.

Commit 2aab8ca

Browse files
Add connectivity option for web with connectivity plugin
1 parent 85cb1af commit 2aab8ca

File tree

6 files changed

+113
-2
lines changed

6 files changed

+113
-2
lines changed

example/ios/Flutter/Debug.xcconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
#include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig"
12
#include "Generated.xcconfig"

example/ios/Flutter/Release.xcconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
#include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig"
12
#include "Generated.xcconfig"

example/pubspec.lock

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,34 @@ packages:
4343
url: "https://pub.dartlang.org"
4444
source: hosted
4545
version: "1.15.0"
46+
connectivity:
47+
dependency: transitive
48+
description:
49+
name: connectivity
50+
url: "https://pub.dartlang.org"
51+
source: hosted
52+
version: "3.0.6"
53+
connectivity_for_web:
54+
dependency: transitive
55+
description:
56+
name: connectivity_for_web
57+
url: "https://pub.dartlang.org"
58+
source: hosted
59+
version: "0.4.0+1"
60+
connectivity_macos:
61+
dependency: transitive
62+
description:
63+
name: connectivity_macos
64+
url: "https://pub.dartlang.org"
65+
source: hosted
66+
version: "0.2.1+2"
67+
connectivity_platform_interface:
68+
dependency: transitive
69+
description:
70+
name: connectivity_platform_interface
71+
url: "https://pub.dartlang.org"
72+
source: hosted
73+
version: "2.0.1"
4674
connectivity_wrapper:
4775
dependency: "direct main"
4876
description:
@@ -74,6 +102,18 @@ packages:
74102
description: flutter
75103
source: sdk
76104
version: "0.0.0"
105+
flutter_web_plugins:
106+
dependency: transitive
107+
description: flutter
108+
source: sdk
109+
version: "0.0.0"
110+
js:
111+
dependency: transitive
112+
description:
113+
name: js
114+
url: "https://pub.dartlang.org"
115+
source: hosted
116+
version: "0.6.3"
77117
matcher:
78118
dependency: transitive
79119
description:
@@ -102,6 +142,13 @@ packages:
102142
url: "https://pub.dartlang.org"
103143
source: hosted
104144
version: "1.8.0"
145+
plugin_platform_interface:
146+
dependency: transitive
147+
description:
148+
name: plugin_platform_interface
149+
url: "https://pub.dartlang.org"
150+
source: hosted
151+
version: "2.0.2"
105152
provider:
106153
dependency: transitive
107154
description:
@@ -172,4 +219,4 @@ packages:
172219
version: "2.1.0"
173220
sdks:
174221
dart: ">=2.12.0 <3.0.0"
175-
flutter: ">=1.16.0"
222+
flutter: ">=1.20.0"

lib/connectivity_wrapper.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import 'dart:io';
1616

1717
// Project imports:
1818
import 'package:connectivity_wrapper/src/utils/constants.dart';
19+
import 'package:flutter/foundation.dart';
20+
import 'package:connectivity/connectivity.dart';
1921

2022
export 'package:connectivity_wrapper/src/widgets/connectivity_app_wrapper_widget.dart';
2123
export 'package:connectivity_wrapper/src/widgets/connectivity_screen_wrapper.dart';
@@ -83,6 +85,7 @@ class ConnectivityWrapper {
8385
List<AddressCheckResult> _lastTryResults = <AddressCheckResult>[];
8486

8587
Future<bool> get isConnected async {
88+
if (kIsWeb) return await _checkWebConnection();
8689
List<Future<AddressCheckResult>> requests = [];
8790

8891
for (var addressOptions in addresses) {
@@ -99,6 +102,16 @@ class ConnectivityWrapper {
99102
: ConnectivityStatus.DISCONNECTED;
100103
}
101104

105+
///
106+
Future<bool> _checkWebConnection() async {
107+
var connectivityResult = await (Connectivity().checkConnectivity());
108+
if (connectivityResult == ConnectivityResult.mobile ||
109+
connectivityResult == ConnectivityResult.wifi) {
110+
return true;
111+
}
112+
return false;
113+
}
114+
102115
Duration checkInterval = DEFAULT_INTERVAL;
103116

104117
_maybeEmitStatusUpdate([Timer? timer]) async {

pubspec.lock

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,34 @@ packages:
5050
url: "https://pub.dartlang.org"
5151
source: hosted
5252
version: "1.15.0"
53+
connectivity:
54+
dependency: "direct main"
55+
description:
56+
name: connectivity
57+
url: "https://pub.dartlang.org"
58+
source: hosted
59+
version: "3.0.6"
60+
connectivity_for_web:
61+
dependency: transitive
62+
description:
63+
name: connectivity_for_web
64+
url: "https://pub.dartlang.org"
65+
source: hosted
66+
version: "0.4.0+1"
67+
connectivity_macos:
68+
dependency: transitive
69+
description:
70+
name: connectivity_macos
71+
url: "https://pub.dartlang.org"
72+
source: hosted
73+
version: "0.2.1+2"
74+
connectivity_platform_interface:
75+
dependency: transitive
76+
description:
77+
name: connectivity_platform_interface
78+
url: "https://pub.dartlang.org"
79+
source: hosted
80+
version: "2.0.1"
5381
fake_async:
5482
dependency: transitive
5583
description:
@@ -67,13 +95,25 @@ packages:
6795
description: flutter
6896
source: sdk
6997
version: "0.0.0"
98+
flutter_web_plugins:
99+
dependency: transitive
100+
description: flutter
101+
source: sdk
102+
version: "0.0.0"
70103
import_sorter:
71104
dependency: "direct dev"
72105
description:
73106
name: import_sorter
74107
url: "https://pub.dartlang.org"
75108
source: hosted
76109
version: "4.5.0"
110+
js:
111+
dependency: transitive
112+
description:
113+
name: js
114+
url: "https://pub.dartlang.org"
115+
source: hosted
116+
version: "0.6.3"
77117
matcher:
78118
dependency: transitive
79119
description:
@@ -102,6 +142,13 @@ packages:
102142
url: "https://pub.dartlang.org"
103143
source: hosted
104144
version: "1.8.0"
145+
plugin_platform_interface:
146+
dependency: transitive
147+
description:
148+
name: plugin_platform_interface
149+
url: "https://pub.dartlang.org"
150+
source: hosted
151+
version: "2.0.2"
105152
provider:
106153
dependency: "direct main"
107154
description:
@@ -186,4 +233,4 @@ packages:
186233
version: "3.1.0"
187234
sdks:
188235
dart: ">=2.12.0 <3.0.0"
189-
flutter: ">=1.16.0"
236+
flutter: ">=1.20.0"

pubspec.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ dependencies:
1111
flutter:
1212
sdk: flutter
1313
provider: ^5.0.0
14+
connectivity: ^3.0.6 # To check connectivity for web only
15+
1416

1517
dev_dependencies:
1618
flutter_test:

0 commit comments

Comments
 (0)