From 6f3cd6b038b783124205fffd2f15a0e6b77e9600 Mon Sep 17 00:00:00 2001 From: Jogboms Date: Fri, 5 Mar 2021 07:04:26 +0100 Subject: [PATCH 1/2] Migrate to null safety --- example/.flutter-plugins-dependencies | 2 +- example/lib/demo_page.dart | 4 +-- example/pubspec.yaml | 5 +-- lib/src/main.dart | 40 +++++++++++----------- lib/src/utils.dart | 8 ++--- pubspec.lock | 37 +++++---------------- pubspec.yaml | 11 +++--- test/flutter_offline_test.dart | 48 ++++----------------------- test/utils_debounce_test.dart | 21 +++++++----- test/utils_starts_with_test.dart | 18 +++++----- 10 files changed, 72 insertions(+), 122 deletions(-) diff --git a/example/.flutter-plugins-dependencies b/example/.flutter-plugins-dependencies index 5217a3f..b19efe2 100644 --- a/example/.flutter-plugins-dependencies +++ b/example/.flutter-plugins-dependencies @@ -1 +1 @@ -{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"connectivity","path":"/Users/jogboms/flutter/.pub-cache/hosted/pub.dartlang.org/connectivity-2.0.2/","dependencies":[]},{"name":"wifi_info_flutter","path":"/Users/jogboms/flutter/.pub-cache/hosted/pub.dartlang.org/wifi_info_flutter-1.0.4/","dependencies":[]}],"android":[{"name":"connectivity","path":"/Users/jogboms/flutter/.pub-cache/hosted/pub.dartlang.org/connectivity-2.0.2/","dependencies":[]},{"name":"wifi_info_flutter","path":"/Users/jogboms/flutter/.pub-cache/hosted/pub.dartlang.org/wifi_info_flutter-1.0.4/","dependencies":[]}],"macos":[{"name":"connectivity_macos","path":"/Users/jogboms/flutter/.pub-cache/hosted/pub.dartlang.org/connectivity_macos-0.1.0+7/","dependencies":[]}],"linux":[],"windows":[],"web":[{"name":"connectivity_for_web","path":"/Users/jogboms/flutter/.pub-cache/hosted/pub.dartlang.org/connectivity_for_web-0.3.1+4/","dependencies":[]}]},"dependencyGraph":[{"name":"connectivity","dependencies":["connectivity_macos","connectivity_for_web"]},{"name":"connectivity_for_web","dependencies":[]},{"name":"connectivity_macos","dependencies":[]},{"name":"wifi_info_flutter","dependencies":[]}],"date_created":"2021-03-05 06:20:56.934344","version":"2.1.0-10.0.pre"} \ No newline at end of file +{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"connectivity","path":"/Users/jogboms/flutter/.pub-cache/hosted/pub.dartlang.org/connectivity-3.0.2/","dependencies":[]},{"name":"wifi_info_flutter","path":"/Users/jogboms/flutter/.pub-cache/hosted/pub.dartlang.org/wifi_info_flutter-2.0.0/","dependencies":[]}],"android":[{"name":"connectivity","path":"/Users/jogboms/flutter/.pub-cache/hosted/pub.dartlang.org/connectivity-3.0.2/","dependencies":[]},{"name":"wifi_info_flutter","path":"/Users/jogboms/flutter/.pub-cache/hosted/pub.dartlang.org/wifi_info_flutter-2.0.0/","dependencies":[]}],"macos":[{"name":"connectivity_macos","path":"/Users/jogboms/flutter/.pub-cache/hosted/pub.dartlang.org/connectivity_macos-0.2.0/","dependencies":[]}],"linux":[],"windows":[],"web":[]},"dependencyGraph":[{"name":"connectivity","dependencies":["connectivity_macos"]},{"name":"connectivity_macos","dependencies":[]},{"name":"wifi_info_flutter","dependencies":[]}],"date_created":"2021-03-05 06:49:22.633197","version":"2.1.0-10.0.pre"} \ No newline at end of file diff --git a/example/lib/demo_page.dart b/example/lib/demo_page.dart index b20a521..bb95e1e 100644 --- a/example/lib/demo_page.dart +++ b/example/lib/demo_page.dart @@ -2,8 +2,8 @@ import 'package:flutter/material.dart'; class DemoPage extends StatelessWidget { const DemoPage({ - Key key, - @required this.child, + Key? key, + required this.child, }) : super(key: key); final Widget child; diff --git a/example/pubspec.yaml b/example/pubspec.yaml index dde5687..4ec3150 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -5,7 +5,8 @@ version: 1.0.0+1 publish_to: none environment: - sdk: ">=2.0.0 <3.0.0" + sdk: '>=2.12.0 <3.0.0' + flutter: ">=2.0.0" dependencies: flutter: @@ -16,7 +17,7 @@ dependencies: dev_dependencies: flutter_test: sdk: flutter - pedantic: ^1.9.0 + pedantic: ^1.11.0 flutter: uses-material-design: true diff --git a/lib/src/main.dart b/lib/src/main.dart index 9b27385..2a36a0c 100644 --- a/lib/src/main.dart +++ b/lib/src/main.dart @@ -6,15 +6,16 @@ import 'package:flutter_offline/src/utils.dart'; import 'package:wifi_info_flutter/wifi_info_flutter.dart'; const kOfflineDebounceDuration = Duration(seconds: 3); +typedef ValueWidgetBuilder = Widget Function(BuildContext context, T value, Widget child); class OfflineBuilder extends StatefulWidget { factory OfflineBuilder({ - Key key, - @required ValueWidgetBuilder connectivityBuilder, + Key? key, + required ValueWidgetBuilder connectivityBuilder, Duration debounceDuration = kOfflineDebounceDuration, - WidgetBuilder builder, - Widget child, - WidgetBuilder errorBuilder, + WidgetBuilder? builder, + Widget? child, + WidgetBuilder? errorBuilder, }) { return OfflineBuilder.initialize( key: key, @@ -23,25 +24,22 @@ class OfflineBuilder extends StatefulWidget { wifiInfo: WifiInfo(), debounceDuration: debounceDuration, builder: builder, - child: child, errorBuilder: errorBuilder, + child: child, ); } @visibleForTesting OfflineBuilder.initialize({ - Key key, - @required this.connectivityBuilder, - @required this.connectivityService, - @required this.wifiInfo, + Key? key, + required this.connectivityBuilder, + required this.connectivityService, + required this.wifiInfo, this.debounceDuration = kOfflineDebounceDuration, this.builder, this.child, this.errorBuilder, - }) : assert(connectivityBuilder != null, 'connectivityBuilder cannot be null'), - assert(debounceDuration != null, 'debounceDuration cannot be null'), - assert(connectivityService != null, 'connectivityService cannot be null'), - assert(!(builder is WidgetBuilder && child is Widget) && !(builder == null && child == null), + }) : assert(!(builder is WidgetBuilder && child is Widget) && !(builder == null && child == null), 'You should specify either a builder or a child'), super(key: key); @@ -57,20 +55,20 @@ class OfflineBuilder extends StatefulWidget { final ValueWidgetBuilder connectivityBuilder; /// Used for building the child widget - final WidgetBuilder builder; + final WidgetBuilder? builder; /// The widget below this widget in the tree. - final Widget child; + final Widget? child; /// Used for building the error widget incase of any platform errors - final WidgetBuilder errorBuilder; + final WidgetBuilder? errorBuilder; @override OfflineBuilderState createState() => OfflineBuilderState(); } class OfflineBuilderState extends State { - Stream _connectivityStream; + late Stream _connectivityStream; @override void initState() { @@ -92,12 +90,12 @@ class OfflineBuilderState extends State { if (snapshot.hasError) { if (widget.errorBuilder != null) { - return widget.errorBuilder(context); + return widget.errorBuilder!(context); } - throw OfflineBuilderError(snapshot.error); + throw OfflineBuilderError(snapshot.error!); } - return widget.connectivityBuilder(context, snapshot.data, widget.child ?? widget.builder(context)); + return widget.connectivityBuilder(context, snapshot.data!, widget.child ?? widget.builder!(context)); }, ); } diff --git a/lib/src/utils.dart b/lib/src/utils.dart index a66d7ae..2e58458 100644 --- a/lib/src/utils.dart +++ b/lib/src/utils.dart @@ -6,7 +6,7 @@ StreamTransformer debounce( Duration debounceDuration, ) { var _seenFirstData = false; - Timer _debounceTimer; + Timer? _debounceTimer; return StreamTransformer.fromHandlers( handleData: (ConnectivityResult data, EventSink sink) { @@ -33,13 +33,13 @@ StreamTransformer startsWith( Stream input, bool cancelOnError, ) { - StreamController controller; - StreamSubscription subscription; + StreamController? controller; + late StreamSubscription subscription; controller = StreamController( sync: true, onListen: () => controller?.add(data), - onPause: ([Future resumeSignal]) => subscription.pause(resumeSignal), + onPause: ([Future? resumeSignal]) => subscription.pause(resumeSignal), onResume: () => subscription.resume(), onCancel: () => subscription.cancel(), ); diff --git a/pubspec.lock b/pubspec.lock index 5909414..195c0c7 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -49,28 +49,21 @@ packages: name: connectivity url: "https://pub.dartlang.org" source: hosted - version: "2.0.2" - connectivity_for_web: - dependency: transitive - description: - name: connectivity_for_web - url: "https://pub.dartlang.org" - source: hosted - version: "0.3.1+4" + version: "3.0.2" connectivity_macos: dependency: transitive description: name: connectivity_macos url: "https://pub.dartlang.org" source: hosted - version: "0.1.0+7" + version: "0.2.0" connectivity_platform_interface: dependency: transitive description: name: connectivity_platform_interface url: "https://pub.dartlang.org" source: hosted - version: "1.0.6" + version: "2.0.1" fake_async: dependency: transitive description: @@ -88,18 +81,6 @@ packages: description: flutter source: sdk version: "0.0.0" - flutter_web_plugins: - dependency: transitive - description: flutter - source: sdk - version: "0.0.0" - js: - dependency: transitive - description: - name: js - url: "https://pub.dartlang.org" - source: hosted - version: "0.6.3" matcher: dependency: transitive description: @@ -127,14 +108,14 @@ packages: name: pedantic url: "https://pub.dartlang.org" source: hosted - version: "1.9.2" + version: "1.11.0" plugin_platform_interface: dependency: transitive description: name: plugin_platform_interface url: "https://pub.dartlang.org" source: hosted - version: "1.0.3" + version: "2.0.0" sky_engine: dependency: transitive description: flutter @@ -202,14 +183,14 @@ packages: name: wifi_info_flutter url: "https://pub.dartlang.org" source: hosted - version: "1.0.4" + version: "2.0.0" wifi_info_flutter_platform_interface: dependency: transitive description: name: wifi_info_flutter_platform_interface url: "https://pub.dartlang.org" source: hosted - version: "1.0.0" + version: "2.0.1" sdks: - dart: ">=2.12.0-0.0 <3.0.0" - flutter: ">=1.20.0" + dart: ">=2.12.0 <3.0.0" + flutter: ">=2.0.0" diff --git a/pubspec.yaml b/pubspec.yaml index 42760db..3471a83 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,18 +1,19 @@ name: flutter_offline description: A tidy utility to handle offline/online connectivity like a Boss. -version: 1.0.0 +version: 1.0.0-nullsafety.0 homepage: https://github.com/jogboms/flutter_offline environment: - sdk: ">=2.1.0 <3.0.0" + sdk: '>=2.12.0 <3.0.0' + flutter: ">=2.0.0" dependencies: flutter: sdk: flutter - connectivity: ^2.0.0 - wifi_info_flutter: ^1.0.0 + connectivity: ^3.0.2 + wifi_info_flutter: ^2.0.0 dev_dependencies: flutter_test: sdk: flutter - pedantic: ^1.9.0 + pedantic: ^1.11.0 diff --git a/test/flutter_offline_test.dart b/test/flutter_offline_test.dart index 8893307..28228c1 100644 --- a/test/flutter_offline_test.dart +++ b/test/flutter_offline_test.dart @@ -46,40 +46,6 @@ void main() { }); group('Test Assertions', () { - testWidgets('Test no debounceDuration param', (WidgetTester tester) async { - expect(() { - OfflineBuilder.initialize( - connectivityService: TestConnectivityService(ConnectivityResult.none), - wifiInfo: TestWifiInfoService(), - connectivityBuilder: (_, __, Widget child) => child, - debounceDuration: null, - builder: (BuildContext context) => Text('builder_result'), - ); - }, throwsAssertionError); - }); - - testWidgets('Test no connectivityBuilder param', (WidgetTester tester) async { - expect(() { - OfflineBuilder.initialize( - connectivityService: TestConnectivityService(ConnectivityResult.none), - wifiInfo: TestWifiInfoService(), - connectivityBuilder: null, - child: const Text('child_result'), - ); - }, throwsAssertionError); - }); - - testWidgets('Test no connectivityService param', (WidgetTester tester) async { - expect(() { - OfflineBuilder.initialize( - connectivityService: null, - wifiInfo: null, - connectivityBuilder: (_, __, Widget child) => child, - child: const Text('child_result'), - ); - }, throwsAssertionError); - }); - testWidgets('Test builder & child param', (WidgetTester tester) async { expect(() { OfflineBuilder.initialize( @@ -257,9 +223,9 @@ void main() { connectivityService: service, wifiInfo: TestWifiInfoService(), connectivityBuilder: (_, ConnectivityResult connectivity, __) => Text('$connectivity'), - child: const SizedBox(), debounceDuration: Duration.zero, errorBuilder: (context) => Text('Error'), + child: const SizedBox(), ), )); @@ -274,16 +240,16 @@ void main() { } class TestConnectivityService implements Connectivity { - TestConnectivityService([this.initialConnection]) { - _result = initialConnection; + TestConnectivityService([this.initialConnection]) : _result = initialConnection ?? ConnectivityResult.none { controller = StreamController.broadcast( onListen: () => controller.add(_result), ); } - StreamController controller; - ConnectivityResult _result = ConnectivityResult.none; - final ConnectivityResult initialConnection; + late final StreamController controller; + final ConnectivityResult? initialConnection; + + ConnectivityResult _result; set result(ConnectivityResult result) { _result = result; @@ -297,7 +263,7 @@ class TestConnectivityService implements Connectivity { @override Future checkConnectivity() { - return Future.delayed(Duration.zero, () => initialConnection); + return Future.delayed(Duration.zero, () => initialConnection!); } } diff --git a/test/utils_debounce_test.dart b/test/utils_debounce_test.dart index 0b40a4d..b822654 100644 --- a/test/utils_debounce_test.dart +++ b/test/utils_debounce_test.dart @@ -4,7 +4,10 @@ import 'package:connectivity/connectivity.dart'; import 'package:flutter_offline/src/utils.dart'; import 'package:flutter_test/flutter_test.dart'; -Future waitForTimer(int milliseconds) => Future(() {/* ensure Timer is started*/}).then( +Future waitForTimer(int milliseconds) => Future(() { + /* ensure Timer is started*/ + }) + .then( (_) => Future.delayed(Duration(milliseconds: milliseconds + 1)), ); @@ -12,13 +15,13 @@ void main() { final stream = () => StreamController.broadcast(); group('Group', () { - StreamController values; - List emittedValues; - bool valuesCanceled; - bool isDone; - List errors; - StreamSubscription subscription; - Stream transformed; + late StreamController values; + late List emittedValues; + late bool valuesCanceled; + late bool isDone; + late List errors; + late StreamSubscription subscription; + late Stream transformed; void setUpStreams(StreamTransformer transformer) { valuesCanceled = false; @@ -29,7 +32,7 @@ void main() { emittedValues = []; errors = []; isDone = false; - transformed = values.stream.transform(transformer); + transformed = values.stream.transform(transformer as StreamTransformer); subscription = transformed.listen(emittedValues.add, onError: errors.add, onDone: () { isDone = true; }); diff --git a/test/utils_starts_with_test.dart b/test/utils_starts_with_test.dart index f12efc8..52fb4fe 100644 --- a/test/utils_starts_with_test.dart +++ b/test/utils_starts_with_test.dart @@ -7,14 +7,14 @@ import 'package:flutter_test/flutter_test.dart'; void main() { final stream = () => StreamController(); - StreamController values; - List emittedValues; - bool valuesCanceled; - bool valuesPaused; - bool valuesResume; - StreamSubscription subscription; + late StreamController values; + late List emittedValues; + late bool valuesCanceled; + late bool valuesPaused; + late bool valuesResume; + late StreamSubscription subscription; // bool isDone; - List errors; + late List errors; void setupForStreamType(StreamTransformer transformer) { emittedValues = []; @@ -32,7 +32,7 @@ void main() { valuesCanceled = true; }; subscription = values.stream - .transform(transformer) + .transform(transformer as StreamTransformer) .listen(emittedValues.add, onError: errors.add, onDone: () { // isDone = true; }); @@ -56,7 +56,7 @@ void main() { }); test('addError values', () async { - values..addError(45); + values.addError(45); await Future(() {}); expect(errors.length, isNonZero); }); From 1c48074ec8c9be2c8ffa81c62d11aca5c24a5688 Mon Sep 17 00:00:00 2001 From: Jogboms Date: Fri, 5 Mar 2021 07:08:53 +0100 Subject: [PATCH 2/2] v2.0 --- CHANGELOG.md | 4 ++++ README.md | 2 +- pubspec.yaml | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 83ec5c7..1f007a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## [2.0.0] + +- Migrate to null-safety + ## [1.0.0] - Improve network and wifi detection diff --git a/README.md b/README.md index 1b5569e..782ddf1 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ A tidy utility to handle offline/online connectivity like a Boss. It provides su ```yaml dependencies: - flutter_offline: "^1.0.0" + flutter_offline: "^2.0.0" ``` ### ⚡️ Import diff --git a/pubspec.yaml b/pubspec.yaml index 3471a83..bbce050 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: flutter_offline description: A tidy utility to handle offline/online connectivity like a Boss. -version: 1.0.0-nullsafety.0 +version: 2.0.0 homepage: https://github.com/jogboms/flutter_offline environment: