diff --git a/.gitignore b/.gitignore index 9d7edcf..c4ce1ac 100644 --- a/.gitignore +++ b/.gitignore @@ -26,6 +26,7 @@ .pub-cache/ .pub/ build/ +*.lock # Android related **/android/**/gradle-wrapper.jar diff --git a/CHANGELOG.md b/CHANGELOG.md index a5b4612..2b8bed7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +#Changelog + +## [1.0.0] +* Null safety. + ## [0.2.0] * Two pinging methods: ping ip addresses one by one OR all at once. diff --git a/README.md b/README.md index 8fcad2e..8d82dab 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # ping_discover_network +Changed to null safety. + ![Pub](https://img.shields.io/pub/v/ping_discover_network.svg) [Dart](https://dart.dev)/[Flutter](https://flutter.dev) library that allows to ping IP subnet and discover network devices. diff --git a/example/flutter_example/.flutter-plugins-dependencies b/example/flutter_example/.flutter-plugins-dependencies new file mode 100644 index 0000000..4a45df7 --- /dev/null +++ b/example/flutter_example/.flutter-plugins-dependencies @@ -0,0 +1 @@ +{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"network_info_plus","path":"E:\\\\Android\\\\flutter-dev\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\network_info_plus-2.1.3\\\\","dependencies":[]}],"android":[{"name":"network_info_plus","path":"E:\\\\Android\\\\flutter-dev\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\network_info_plus-2.1.3\\\\","dependencies":[]}],"macos":[{"name":"network_info_plus_macos","path":"E:\\\\Android\\\\flutter-dev\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\network_info_plus_macos-1.3.0\\\\","dependencies":[]}],"linux":[{"name":"network_info_plus_linux","path":"E:\\\\Android\\\\flutter-dev\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\network_info_plus_linux-1.1.2\\\\","dependencies":[]}],"windows":[{"name":"network_info_plus_windows","path":"E:\\\\Android\\\\flutter-dev\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\network_info_plus_windows-1.0.2\\\\","dependencies":[]}],"web":[{"name":"network_info_plus_web","path":"E:\\\\Android\\\\flutter-dev\\\\.pub-cache\\\\hosted\\\\pub.dartlang.org\\\\network_info_plus_web-1.0.1\\\\","dependencies":[]}]},"dependencyGraph":[{"name":"network_info_plus","dependencies":["network_info_plus_linux","network_info_plus_macos","network_info_plus_windows","network_info_plus_web"]},{"name":"network_info_plus_linux","dependencies":[]},{"name":"network_info_plus_macos","dependencies":[]},{"name":"network_info_plus_web","dependencies":[]},{"name":"network_info_plus_windows","dependencies":[]}],"date_created":"2022-03-01 15:17:03.215673","version":"2.10.0-0.3.pre"} \ No newline at end of file diff --git a/example/flutter_example/android/app/src/main/AndroidManifest.xml b/example/flutter_example/android/app/src/main/AndroidManifest.xml index b8c4588..e08ad51 100644 --- a/example/flutter_example/android/app/src/main/AndroidManifest.xml +++ b/example/flutter_example/android/app/src/main/AndroidManifest.xml @@ -1,13 +1,7 @@ - - - - + + diff --git a/example/flutter_example/ios/Flutter/flutter_export_environment.sh b/example/flutter_example/ios/Flutter/flutter_export_environment.sh new file mode 100644 index 0000000..7d8a5b8 --- /dev/null +++ b/example/flutter_example/ios/Flutter/flutter_export_environment.sh @@ -0,0 +1,13 @@ +#!/bin/sh +# This is a generated file; do not edit or check into version control. +export "FLUTTER_ROOT=E:\Android\flutter-dev" +export "FLUTTER_APPLICATION_PATH=E:\Android\Projects\ping_discover_network\example\flutter_example" +export "COCOAPODS_PARALLEL_CODE_SIGN=true" +export "FLUTTER_TARGET=lib\main.dart" +export "FLUTTER_BUILD_DIR=build" +export "FLUTTER_BUILD_NAME=1.0.0" +export "FLUTTER_BUILD_NUMBER=1" +export "DART_OBFUSCATION=false" +export "TRACK_WIDGET_CREATION=false" +export "TREE_SHAKE_ICONS=false" +export "PACKAGE_CONFIG=.packages" diff --git a/example/flutter_example/lib/main.dart b/example/flutter_example/lib/main.dart index 293626b..a63eecc 100644 --- a/example/flutter_example/lib/main.dart +++ b/example/flutter_example/lib/main.dart @@ -1,6 +1,8 @@ +import 'dart:io'; + import 'package:flutter/material.dart'; +import 'package:network_info_plus/network_info_plus.dart'; import 'package:ping_discover_network/ping_discover_network.dart'; -import 'package:wifi/wifi.dart'; void main() => runApp(MyApp()); @@ -38,12 +40,11 @@ class _MyHomePageState extends State { String ip; try { - ip = await Wifi.ip; + ip = (await NetworkInfo().getWifiIP())!; print('local ip:\t$ip'); } catch (e) { - final snackBar = SnackBar( - content: Text('WiFi is not connected', textAlign: TextAlign.center)); - Scaffold.of(ctx).showSnackBar(snackBar); + final snackBar = SnackBar(content: Text('WiFi is not connected', textAlign: TextAlign.center)); + ScaffoldMessenger.of(ctx).showSnackBar(snackBar); return; } setState(() { @@ -77,12 +78,36 @@ class _MyHomePageState extends State { }); }) ..onError((dynamic e) { - final snackBar = SnackBar( - content: Text('Unexpected exception', textAlign: TextAlign.center)); - Scaffold.of(ctx).showSnackBar(snackBar); + final snackBar = SnackBar(content: Text('Unexpected exception', textAlign: TextAlign.center)); + ScaffoldMessenger.of(ctx).showSnackBar(snackBar); }); } + // https://stackoverflow.com/questions/63514434/flutter-get-local-ip-address-on-android + Future _getLocalIpAddress() async { + final interfaces = await NetworkInterface.list(type: InternetAddressType.IPv4, includeLinkLocal: true); + + try { + // Try VPN connection first + final vpnInterface = interfaces.firstWhere((element) => element.name == 'tun0'); + return vpnInterface.addresses.first.address; + } on StateError { + // Try wlan connection next + try { + final interface = interfaces.firstWhere((element) => element.name == 'wlan0'); + return interface.addresses.first.address; + } catch (ex) { + // Try any other connection next + try { + final interface = interfaces.firstWhere((element) => !(element.name == 'tun0' || element.name == 'wlan0')); + return interface.addresses.first.address; + } catch (ex) { + return ''; + } + } + } + } + @override Widget build(BuildContext context) { return Scaffold( @@ -107,15 +132,9 @@ class _MyHomePageState extends State { SizedBox(height: 10), Text('Local ip: $localIp', style: TextStyle(fontSize: 16)), SizedBox(height: 15), - RaisedButton( - child: Text( - '${isDiscovering ? 'Discovering...' : 'Discover'}'), - onPressed: isDiscovering ? null : () => discover(context)), + ElevatedButton(child: Text('${isDiscovering ? 'Discovering...' : 'Discover'}'), onPressed: isDiscovering ? null : () => discover(context)), SizedBox(height: 15), - found >= 0 - ? Text('Found: $found device(s)', - style: TextStyle(fontSize: 16)) - : Container(), + found >= 0 ? Text('Found: $found device(s)', style: TextStyle(fontSize: 16)) : Container(), Expanded( child: ListView.builder( itemCount: devices.length, @@ -132,8 +151,7 @@ class _MyHomePageState extends State { SizedBox(width: 10), Expanded( child: Column( - crossAxisAlignment: - CrossAxisAlignment.start, + crossAxisAlignment: CrossAxisAlignment.start, mainAxisAlignment: MainAxisAlignment.center, children: [ Text( diff --git a/example/flutter_example/pubspec.lock b/example/flutter_example/pubspec.lock new file mode 100644 index 0000000..202edd3 --- /dev/null +++ b/example/flutter_example/pubspec.lock @@ -0,0 +1,271 @@ +# Generated by pub +# See https://dart.dev/tools/pub/glossary#lockfile +packages: + args: + dependency: transitive + description: + name: args + url: "https://pub.dartlang.org" + source: hosted + version: "2.3.0" + async: + dependency: transitive + description: + name: async + url: "https://pub.dartlang.org" + source: hosted + version: "2.8.2" + boolean_selector: + dependency: transitive + description: + name: boolean_selector + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.0" + characters: + dependency: transitive + description: + name: characters + url: "https://pub.dartlang.org" + source: hosted + version: "1.2.0" + charcode: + dependency: transitive + description: + name: charcode + url: "https://pub.dartlang.org" + source: hosted + version: "1.3.1" + clock: + dependency: transitive + description: + name: clock + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.0" + collection: + dependency: transitive + description: + name: collection + url: "https://pub.dartlang.org" + source: hosted + version: "1.15.0" + cupertino_icons: + dependency: "direct main" + description: + name: cupertino_icons + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.4" + dbus: + dependency: transitive + description: + name: dbus + url: "https://pub.dartlang.org" + source: hosted + version: "0.7.1" + fake_async: + dependency: transitive + description: + name: fake_async + url: "https://pub.dartlang.org" + source: hosted + version: "1.2.0" + ffi: + dependency: transitive + description: + name: ffi + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.2" + flutter: + dependency: "direct main" + description: flutter + source: sdk + version: "0.0.0" + flutter_test: + dependency: "direct dev" + 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: + name: matcher + url: "https://pub.dartlang.org" + source: hosted + version: "0.12.11" + material_color_utilities: + dependency: transitive + description: + name: material_color_utilities + url: "https://pub.dartlang.org" + source: hosted + version: "0.1.3" + meta: + dependency: transitive + description: + name: meta + url: "https://pub.dartlang.org" + source: hosted + version: "1.7.0" + network_info_plus: + dependency: "direct main" + description: + name: network_info_plus + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.3" + network_info_plus_linux: + dependency: transitive + description: + name: network_info_plus_linux + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.2" + network_info_plus_macos: + dependency: transitive + description: + name: network_info_plus_macos + url: "https://pub.dartlang.org" + source: hosted + version: "1.3.0" + network_info_plus_platform_interface: + dependency: transitive + description: + name: network_info_plus_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.2" + network_info_plus_web: + dependency: transitive + description: + name: network_info_plus_web + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.1" + network_info_plus_windows: + dependency: transitive + description: + name: network_info_plus_windows + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.2" + nm: + dependency: transitive + description: + name: nm + url: "https://pub.dartlang.org" + source: hosted + version: "0.5.0" + path: + dependency: transitive + description: + name: path + url: "https://pub.dartlang.org" + source: hosted + version: "1.8.0" + petitparser: + dependency: transitive + description: + name: petitparser + url: "https://pub.dartlang.org" + source: hosted + version: "4.4.0" + ping_discover_network: + dependency: "direct main" + description: + path: "../.." + relative: true + source: path + version: "1.0.0" + plugin_platform_interface: + dependency: transitive + description: + name: plugin_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.2" + sky_engine: + dependency: transitive + description: flutter + source: sdk + version: "0.0.99" + source_span: + dependency: transitive + description: + name: source_span + url: "https://pub.dartlang.org" + source: hosted + version: "1.8.1" + stack_trace: + dependency: transitive + description: + name: stack_trace + url: "https://pub.dartlang.org" + source: hosted + version: "1.10.0" + stream_channel: + dependency: transitive + description: + name: stream_channel + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.0" + string_scanner: + dependency: transitive + description: + name: string_scanner + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.0" + term_glyph: + dependency: transitive + description: + name: term_glyph + url: "https://pub.dartlang.org" + source: hosted + version: "1.2.0" + test_api: + dependency: transitive + description: + name: test_api + url: "https://pub.dartlang.org" + source: hosted + version: "0.4.8" + typed_data: + dependency: transitive + description: + name: typed_data + url: "https://pub.dartlang.org" + source: hosted + version: "1.3.0" + vector_math: + dependency: transitive + description: + name: vector_math + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.1" + xml: + dependency: transitive + description: + name: xml + url: "https://pub.dartlang.org" + source: hosted + version: "5.3.1" +sdks: + dart: ">=2.15.0 <3.0.0" + flutter: ">=1.20.0" diff --git a/example/flutter_example/pubspec.yaml b/example/flutter_example/pubspec.yaml index 6ee1810..e15e5c4 100644 --- a/example/flutter_example/pubspec.yaml +++ b/example/flutter_example/pubspec.yaml @@ -4,20 +4,22 @@ description: A new Flutter project. version: 1.0.0+1 environment: - sdk: ">=2.1.0 <3.0.0" + sdk: ">=2.12.0 <3.0.0" + flutter: ">=1.17.0" dependencies: flutter: sdk: flutter - cupertino_icons: ^0.1.2 + + cupertino_icons: ^1.0.2 + ping_discover_network: path: ../../ - wifi: ^0.1.5 + network_info_plus: ^2.1.3 dev_dependencies: flutter_test: sdk: flutter - flutter: uses-material-design: true diff --git a/lib/src/network_analyzer.dart b/lib/src/network_analyzer.dart index b806e14..c465499 100644 --- a/lib/src/network_analyzer.dart +++ b/lib/src/network_analyzer.dart @@ -8,11 +8,14 @@ import 'dart:async'; import 'dart:io'; +// ignore_for_file: avoid_classes_with_only_static_members + /// [NetworkAnalyzer] class returns instances of [NetworkAddress]. /// /// Found ip addresses will have [exists] == true field. class NetworkAddress { NetworkAddress(this.ip, this.exists); + bool exists; String ip; } @@ -45,7 +48,7 @@ class NetworkAnalyzer { } // Check if connection timed out or we got one of predefined errors - if (e.osError == null || _errorCodes.contains(e.osError.errorCode)) { + if (e.osError == null || _errorCodes.contains(e.osError!.errorCode)) { yield NetworkAddress(host, false); } else { // Error 23,24: Too many open files in system @@ -83,7 +86,7 @@ class NetworkAnalyzer { } // Check if connection timed out or we got one of predefined errors - if (e.osError == null || _errorCodes.contains(e.osError.errorCode)) { + if (e.osError == null || _errorCodes.contains(e.osError!.errorCode)) { out.sink.add(NetworkAddress(host, false)); } else { // Error 23,24: Too many open files in system @@ -92,9 +95,7 @@ class NetworkAnalyzer { }); } - Future.wait(futures) - .then((sockets) => out.close()) - .catchError((dynamic e) => out.close()); + Future.wait(futures).then((sockets) => out.close()).catchError((dynamic e) => out.close()); return out.stream; } diff --git a/pubspec.yaml b/pubspec.yaml index 9c95788..f1a72de 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,11 +1,11 @@ name: ping_discover_network description: Library that allows to ping IP subnet and discover network devices. Could be used to find printers and other devices and services in a local network. -version: 0.2.0+1 +version: 1.0.0 author: Andrey Ushakov homepage: https://github.com/andrey-ushakov/ping_discover_network environment: - sdk: ">=2.1.0 <3.0.0" + sdk: ">=2.12.0 <3.0.0" dependencies: flutter: