Skip to content

null-safety for mdns_plugin #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion mdns_plugin/example/lib/src/models/service_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import 'package:mdns_plugin/mdns_plugin.dart';
/////////////////////////////////////////////////////////////////////

class ServiceList {
List<MDNSService> _list = List<MDNSService>();
List<MDNSService> _list = <MDNSService>[];

// METHODS ////////////////////////////////////////////////////////

Expand Down
11 changes: 5 additions & 6 deletions mdns_plugin/example/lib/src/pages/service_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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: <Widget>[
ButtonBar(children: <Widget>[
FlatButton(
child: const Text('RESCAN'),
onPressed: () => appBloc.dispatch(
AppEventDiscovery(AppEventDiscoveryState.Restart)))
]))
child: const Text('RESCAN'),
onPressed: () => appBloc.dispatch(
AppEventDiscovery(AppEventDiscoveryState.Restart)))
])
]));
});
}
56 changes: 24 additions & 32 deletions mdns_plugin/lib/mdns_plugin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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<String?> get addresses {
var addresses = map["address"];

if (addresses is List<dynamic>) {
var address = List<String>();
var address = <String?>[];
addresses.forEach((value) {
if (value.length == 2 && value[0] is String) {
address.add(value[0]);
Expand All @@ -71,13 +71,10 @@ class MDNSService {

/// toUTFString decodes a TXT value into a UTF8 string
static String toUTF8String(List<int> bytes) {
if (bytes == null) {
return null;
} else {
return Utf8Codec().decode(bytes);
}
return Utf8Codec().decode(bytes);
}

@override
String toString() {
var parts = "";
if (name != "") {
Expand All @@ -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' ";
});
Expand All @@ -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"]) {
Expand All @@ -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));
Expand All @@ -146,7 +140,7 @@ class MDNSPlugin {

/// platformVersion returns the underlying platform version of the
/// running plugin
static Future<String> get platformVersion async {
static Future<String?> get platformVersion async {
return await _methodChannel.invokeMethod('getPlatformVersion');
}

Expand All @@ -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<void> startDiscovery(String serviceType,
{bool enableUpdating = false}) async {
return await _methodChannel.invokeMethod("startDiscovery",
{"serviceType": serviceType, "enableUpdating": enableUpdating});
Future<void> startDiscovery(String serviceType, {bool enableUpdating = false}) async {
return await _methodChannel.invokeMethod(
"startDiscovery", {"serviceType": serviceType, "enableUpdating": enableUpdating});
}

/// stopDiscovery should be invoked to shutdown the discovery
Expand All @@ -169,9 +162,8 @@ class MDNSPlugin {

// PRIVATE METHODS ////////////////////////////////////////////////

Future<void> _resolveService(MDNSService service,
{bool resolve = false}) async {
_methodChannel.invokeMethod(
'resolveService', {"name": service.name, "resolve": resolve});
Future<void> _resolveService(MDNSService service, {bool resolve = false}) async {
return _methodChannel
.invokeMethod('resolveService', {"name": service.name, "resolve": resolve});
}
}
2 changes: 1 addition & 1 deletion mdns_plugin/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ author: David Thorpe <[email protected]>
homepage: https://github.com/djthorpe/flutter/

environment:
sdk: ">=2.1.0 <3.0.0"
sdk: '>=2.12.0 <3.0.0'

dependencies:
flutter:
Expand Down