Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/connectivity_plus/connectivity_plus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ import 'package:connectivity_plus/connectivity_plus.dart';

final List<ConnectivityResult> connectivityResult = await (Connectivity().checkConnectivity());

// Determines whether any active network connection exists.
if (connectivityResult.hasConnectivity) {
// Connectivity available (regardless of the underlying transport).
}

// This condition is for demo purposes only to explain every connection type.
// Use conditions which work for your requirements.
if (connectivityResult.contains(ConnectivityResult.mobile)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ class _MyHomePageState extends State<MyHomePage> {
mainAxisSize: MainAxisSize.min,
children: [
const Spacer(flex: 2),
Text(_connectionStatus.hasConnectivity
? 'Connectivity available'
: 'No connectivity'),
const Spacer(flex: 2),
Text(
'Active connection types:',
style: Theme.of(context).textTheme.headlineMedium,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import 'package:collection/collection.dart';

// Export enums from the platform_interface so plugin users can use them directly.
export 'package:connectivity_plus_platform_interface/connectivity_plus_platform_interface.dart'
show ConnectivityResult;
show ConnectivityResult, ConnectivityResultListX;

export 'src/connectivity_plus_linux.dart'
if (dart.library.js_interop) 'src/connectivity_plus_web.dart';
Expand Down Expand Up @@ -46,7 +46,7 @@ class Connectivity {
/// case where [ConnectivityResult.none] is present.
///
/// This method applies [Stream.distinct] over the received events to ensure
/// only emiting when connectivity changes.
/// only emitting when connectivity changes.
Stream<List<ConnectivityResult>> get onConnectivityChanged {
return _platform.onConnectivityChanged.distinct((a, b) => a.equals(b));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,9 @@ enum ConnectivityResult {
/// Other: Device is connected to an unknown network
other
}

extension ConnectivityResultListX on List<ConnectivityResult> {
/// Returns whether any active network connection exists.
bool get hasConnectivity =>
!(length == 1 && first == ConnectivityResult.none);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ dependencies:
dev_dependencies:
flutter_test:
sdk: flutter
test: ^1.31.0
flutter_lints: ">=4.0.0 <6.0.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import 'package:connectivity_plus_platform_interface/src/enums.dart';
import 'package:test/test.dart';

void main() {
group('List<$ConnectivityResult>.hasConnectivity', () {
test('returns false when only none is present', () {
final result = [ConnectivityResult.none];
expect(result.hasConnectivity, false, reason: 'list: $result');
});

test('returns true when wifi is present', () {
final result = [ConnectivityResult.wifi];
expect(result.hasConnectivity, true, reason: 'list: $result');
});

test('returns true when multiple connections exist', () {
final result = ConnectivityResult.values.toList()
..removeWhere((e) => e == ConnectivityResult.none);
expect(result.hasConnectivity, true, reason: 'list: $result');
});
});
}
Loading