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
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,22 @@ class Connectivity {
/// status changes, this is a known issue that only affects simulators.
/// For details see https://github.com/fluttercommunity/plus_plugins/issues/479.
///
/// The emitted list is never empty. In case of no connectivity, the list contains
/// The emitted list is never empty.
/// {@template no_connectivity_case}
/// In case of no connectivity, the list contains
/// a single element of [ConnectivityResult.none]. Note also that this is the only
/// case where [ConnectivityResult.none] is present.
///
/// Use `hasConnectivity` to determine whether any active network connection exists:
///
/// ```dart
/// final result = await checkConnectivity();
/// print(result.hasConnectivity);
/// ```
/// {@endtemplate}
///
/// 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 All @@ -57,9 +67,8 @@ class Connectivity {
/// make a network request, it only gives you the radio status. Instead, listen
/// for connectivity changes via [onConnectivityChanged] stream.
///
/// The returned list is never empty. In case of no connectivity, the list contains
/// a single element of [ConnectivityResult.none]. Note also that this is the only
/// case where [ConnectivityResult.none] is present.
/// The returned list is never empty.
/// {@macro no_connectivity_case}
Future<List<ConnectivityResult>> checkConnectivity() {
return _platform.checkConnectivity();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,8 @@ enum ConnectivityResult {
/// Other: Device is connected to an unknown network
other
}

extension ConnectivityResultListX on List<ConnectivityResult> {
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