Skip to content
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

Null safety #13

Open
wants to merge 4 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
.pub-cache/
.pub/
build/
*.lock

# Android related
**/android/**/gradle-wrapper.jar
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
1 change: 1 addition & 0 deletions example/flutter_example/.flutter-plugins-dependencies
Original file line number Diff line number Diff line change
@@ -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"}
20 changes: 6 additions & 14 deletions example/flutter_example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.flutter_example">

<!-- io.flutter.app.FlutterApplication is an android.app.Application that
calls FlutterMain.startInitialization(this); in its onCreate method.
In most cases you can leave this as-is, but you if you want to provide
additional functionality it is fine to subclass or reimplement
FlutterApplication and put your custom class here. -->
<application
android:name="io.flutter.app.FlutterApplication"
android:name="${applicationName}"
android:label="flutter_example"
android:icon="@mipmap/ic_launcher">
<activity
Expand All @@ -17,18 +11,16 @@
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<!-- This keeps the window background of the activity showing
until Flutter renders its first frame. It can be removed if
there is no splash screen (such as the default splash screen
defined in @style/LaunchTheme). -->
<meta-data
android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
android:value="true" />
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
android:name="flutterEmbedding"
android:value="2" />
</application>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
13 changes: 13 additions & 0 deletions example/flutter_example/ios/Flutter/flutter_export_environment.sh
Original file line number Diff line number Diff line change
@@ -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"
54 changes: 36 additions & 18 deletions example/flutter_example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -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());

Expand Down Expand Up @@ -38,12 +40,11 @@ class _MyHomePageState extends State<MyHomePage> {

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(() {
Expand Down Expand Up @@ -77,12 +78,36 @@ class _MyHomePageState extends State<MyHomePage> {
});
})
..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<String> _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(
Expand All @@ -107,15 +132,9 @@ class _MyHomePageState extends State<MyHomePage> {
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,
Expand All @@ -132,8 +151,7 @@ class _MyHomePageState extends State<MyHomePage> {
SizedBox(width: 10),
Expanded(
child: Column(
crossAxisAlignment:
CrossAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
Expand Down
Loading