From 16a5faf3060049a5c046b1a8258117a0fd3898f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B0=D0=B4=D0=B8=D0=BC=20=D0=A2=D1=83=D1=80=D0=BA?= =?UTF-8?q?=D0=BE?= Date: Thu, 4 Mar 2021 22:38:59 +0300 Subject: [PATCH 1/2] null-safety --- mdns_plugin/analysis_options.yaml | 11 ++++ .../example/lib/src/pages/service_list.dart | 11 ++-- mdns_plugin/lib/mdns_plugin.dart | 56 ++++++++----------- mdns_plugin/pubspec.yaml | 3 +- 4 files changed, 42 insertions(+), 39 deletions(-) create mode 100644 mdns_plugin/analysis_options.yaml diff --git a/mdns_plugin/analysis_options.yaml b/mdns_plugin/analysis_options.yaml new file mode 100644 index 0000000..440aac9 --- /dev/null +++ b/mdns_plugin/analysis_options.yaml @@ -0,0 +1,11 @@ +include: package:pedantic/analysis_options.yaml + +analyzer: + exclude: + # workaround for https://github.com/dart-lang/sdk/issues/42910 + - 'example/**' + +# https://dart-lang.github.io/linter/lints/ +linter: + rules: + prefer_single_quotes: false \ No newline at end of file diff --git a/mdns_plugin/example/lib/src/pages/service_list.dart b/mdns_plugin/example/lib/src/pages/service_list.dart index a8177da..40ba30d 100644 --- a/mdns_plugin/example/lib/src/pages/service_list.dart +++ b/mdns_plugin/example/lib/src/pages/service_list.dart @@ -59,13 +59,12 @@ class ServiceList extends StatelessWidget { title: Text('FOUND CHROMECASTS'), subtitle: Text( "The following devices were found on your local network")), - ButtonTheme.bar( - child: ButtonBar(children: [ + ButtonBar(children: [ FlatButton( - child: const Text('RESCAN'), - onPressed: () => appBloc.dispatch( - AppEventDiscovery(AppEventDiscoveryState.Restart))) - ])) + child: const Text('RESCAN'), + onPressed: () => appBloc.dispatch( + AppEventDiscovery(AppEventDiscoveryState.Restart))) + ]) ])); }); } diff --git a/mdns_plugin/lib/mdns_plugin.dart b/mdns_plugin/lib/mdns_plugin.dart index 45d40fd..0571ed2 100644 --- a/mdns_plugin/lib/mdns_plugin.dart +++ b/mdns_plugin/lib/mdns_plugin.dart @@ -42,20 +42,20 @@ class MDNSService { // CONSTRUCTORS /////////////////////////////////////////////////// - MDNSService.fromMap(this.map) : assert(map != null); + MDNSService.fromMap(this.map); // PROPERTIES ///////////////////////////////////////////////////// - String get name => map["name"]; - String get hostName => map["hostName"]; - String get serviceType => map["type"]; - int get port => map["port"]; - Map get txt => map["txt"]; - List get addresses { + String? get name => map["name"]; + String? get hostName => map["hostName"]; + String? get serviceType => map["type"]; + int? get port => map["port"]; + Map? get txt => map["txt"]; + List get addresses { var addresses = map["address"]; if (addresses is List) { - var address = List(); + var address = []; addresses.forEach((value) { if (value.length == 2 && value[0] is String) { address.add(value[0]); @@ -71,13 +71,10 @@ class MDNSService { /// toUTFString decodes a TXT value into a UTF8 string static String toUTF8String(List bytes) { - if (bytes == null) { - return null; - } else { - return Utf8Codec().decode(bytes); - } + return Utf8Codec().decode(bytes); } + @override String toString() { var parts = ""; if (name != "") { @@ -86,13 +83,13 @@ class MDNSService { if (serviceType != "") { parts = parts + "serviceType='$serviceType' "; } - if (hostName != "" && port > 0) { + if (hostName != "" && port! > 0) { parts = parts + "host='$hostName:$port' "; } - if (addresses.length > 0) { + if (addresses.isNotEmpty) { parts = parts + "addresses=$addresses "; } - txt.forEach((k, v) { + txt!.forEach((k, v) { var vstr = toUTF8String(v); parts = parts + "$k='$vstr' "; }); @@ -105,15 +102,13 @@ class MDNSService { /// MDNSPlugin is the provider of the mDNS discovery from the local /// network class MDNSPlugin { - static const MethodChannel _methodChannel = - const MethodChannel('mdns_plugin'); - static const EventChannel _eventChannel = - const EventChannel('mdns_plugin_delegate'); + static const MethodChannel _methodChannel = MethodChannel('mdns_plugin'); + static const EventChannel _eventChannel = EventChannel('mdns_plugin_delegate'); final MDNSPluginDelegate delegate; // CONSTRUCTORS /////////////////////////////////////////////////// - MDNSPlugin(this.delegate) : assert(delegate != null) { + MDNSPlugin(this.delegate) { _eventChannel.receiveBroadcastStream().listen((args) { if (args is Map && args.containsKey("method")) { switch (args["method"]) { @@ -125,8 +120,7 @@ class MDNSPlugin { break; case "onServiceFound": var service = MDNSService.fromMap(args); - this._resolveService(service, - resolve: delegate.onServiceFound(service)); + _resolveService(service, resolve: delegate.onServiceFound(service)); break; case "onServiceResolved": delegate.onServiceResolved(MDNSService.fromMap(args)); @@ -146,7 +140,7 @@ class MDNSPlugin { /// platformVersion returns the underlying platform version of the /// running plugin - static Future get platformVersion async { + static Future get platformVersion async { return await _methodChannel.invokeMethod('getPlatformVersion'); } @@ -155,10 +149,9 @@ class MDNSPlugin { /// example "_googlecast._tcp" or similar. When the optional /// enableUpdating flag is set to true, resolved services /// respond to updates to the TXT record for the service - Future startDiscovery(String serviceType, - {bool enableUpdating = false}) async { - return await _methodChannel.invokeMethod("startDiscovery", - {"serviceType": serviceType, "enableUpdating": enableUpdating}); + Future startDiscovery(String serviceType, {bool enableUpdating = false}) async { + return await _methodChannel.invokeMethod( + "startDiscovery", {"serviceType": serviceType, "enableUpdating": enableUpdating}); } /// stopDiscovery should be invoked to shutdown the discovery @@ -169,9 +162,8 @@ class MDNSPlugin { // PRIVATE METHODS //////////////////////////////////////////////// - Future _resolveService(MDNSService service, - {bool resolve = false}) async { - _methodChannel.invokeMethod( - 'resolveService', {"name": service.name, "resolve": resolve}); + Future _resolveService(MDNSService service, {bool resolve = false}) async { + return _methodChannel + .invokeMethod('resolveService', {"name": service.name, "resolve": resolve}); } } diff --git a/mdns_plugin/pubspec.yaml b/mdns_plugin/pubspec.yaml index 06bfacc..12d158e 100644 --- a/mdns_plugin/pubspec.yaml +++ b/mdns_plugin/pubspec.yaml @@ -7,7 +7,7 @@ author: David Thorpe homepage: https://github.com/djthorpe/flutter/ environment: - sdk: ">=2.1.0 <3.0.0" + sdk: '>=2.12.0 <3.0.0' dependencies: flutter: @@ -16,6 +16,7 @@ dependencies: dev_dependencies: flutter_test: sdk: flutter + pedantic: ^1.11.0 flutter: plugin: From 7a40c0935345ca51b42d2a0c0d7a9a9845f6c44c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B0=D0=B4=D0=B8=D0=BC=20=D0=A2=D1=83=D1=80=D0=BA?= =?UTF-8?q?=D0=BE?= Date: Thu, 4 Mar 2021 22:40:02 +0300 Subject: [PATCH 2/2] clean up --- mdns_plugin/analysis_options.yaml | 11 ----------- mdns_plugin/example/lib/src/models/service_list.dart | 2 +- mdns_plugin/pubspec.yaml | 1 - 3 files changed, 1 insertion(+), 13 deletions(-) delete mode 100644 mdns_plugin/analysis_options.yaml diff --git a/mdns_plugin/analysis_options.yaml b/mdns_plugin/analysis_options.yaml deleted file mode 100644 index 440aac9..0000000 --- a/mdns_plugin/analysis_options.yaml +++ /dev/null @@ -1,11 +0,0 @@ -include: package:pedantic/analysis_options.yaml - -analyzer: - exclude: - # workaround for https://github.com/dart-lang/sdk/issues/42910 - - 'example/**' - -# https://dart-lang.github.io/linter/lints/ -linter: - rules: - prefer_single_quotes: false \ No newline at end of file diff --git a/mdns_plugin/example/lib/src/models/service_list.dart b/mdns_plugin/example/lib/src/models/service_list.dart index 0e32a3d..95a6728 100644 --- a/mdns_plugin/example/lib/src/models/service_list.dart +++ b/mdns_plugin/example/lib/src/models/service_list.dart @@ -12,7 +12,7 @@ import 'package:mdns_plugin/mdns_plugin.dart'; ///////////////////////////////////////////////////////////////////// class ServiceList { - List _list = List(); + List _list = []; // METHODS //////////////////////////////////////////////////////// diff --git a/mdns_plugin/pubspec.yaml b/mdns_plugin/pubspec.yaml index 12d158e..be747d3 100644 --- a/mdns_plugin/pubspec.yaml +++ b/mdns_plugin/pubspec.yaml @@ -16,7 +16,6 @@ dependencies: dev_dependencies: flutter_test: sdk: flutter - pedantic: ^1.11.0 flutter: plugin: