Skip to content

Commit

Permalink
null safety
Browse files Browse the repository at this point in the history
changed Toast plugin (old not work, 8.x.x have error on compile)
correct library for null safety
add device name in example (without devicename, sample not work)
  • Loading branch information
cor277 committed Apr 4, 2021
1 parent c906388 commit 65676ca
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 36 deletions.
2 changes: 1 addition & 1 deletion example/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:4.0.1'
classpath 'com.android.tools.build:gradle:4.1.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
Expand Down
2 changes: 1 addition & 1 deletion example/android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip
40 changes: 27 additions & 13 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';

import 'package:device_info/device_info.dart';
import 'package:flutter_styled_toast/flutter_styled_toast.dart';
import 'package:flutter/material.dart';
import 'package:flutter_nearby_connections/flutter_nearby_connections.dart';
import 'package:fluttertoast/fluttertoast.dart';

void main() {
runApp(MyApp());
Expand Down Expand Up @@ -85,7 +84,7 @@ class Home extends StatelessWidget {
enum DeviceType { advertiser, browser }

class DevicesListScreen extends StatefulWidget {
const DevicesListScreen({this.deviceType});
const DevicesListScreen({required this.deviceType});

final DeviceType deviceType;

Expand All @@ -96,9 +95,9 @@ class DevicesListScreen extends StatefulWidget {
class _DevicesListScreenState extends State<DevicesListScreen> {
List<Device> devices = [];
List<Device> connectedDevices = [];
NearbyService nearbyService;
StreamSubscription subscription;
StreamSubscription receivedDataSubscription;
late NearbyService nearbyService;
late StreamSubscription subscription;
late StreamSubscription receivedDataSubscription;

bool isInit = false;

Expand All @@ -110,8 +109,8 @@ class _DevicesListScreenState extends State<DevicesListScreen> {

@override
void dispose() {
subscription?.cancel();
receivedDataSubscription?.cancel();
subscription.cancel();
receivedDataSubscription.cancel();
nearbyService.stopBrowsingForPeers();
nearbyService.stopAdvertisingPeer();
super.dispose();
Expand Down Expand Up @@ -237,13 +236,13 @@ class _DevicesListScreenState extends State<DevicesListScreen> {
title: Text("Send message"),
content: TextField(controller: myController),
actions: [
FlatButton(
TextButton(
child: Text("Cancel"),
onPressed: () {
Navigator.of(context).pop();
},
),
FlatButton(
TextButton(
child: Text("Send"),
onPressed: () {
nearbyService.sendMessage(
Expand Down Expand Up @@ -283,8 +282,19 @@ class _DevicesListScreenState extends State<DevicesListScreen> {

void init() async {
nearbyService = NearbyService();
String devInfo = '';
DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
if (Platform.isAndroid) {
AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
devInfo = androidInfo.model;
}
if (Platform.isIOS) {
IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
devInfo = iosInfo.localizedModel;
}
await nearbyService.init(
serviceType: 'mp-connection',
serviceType: 'mpconn',
deviceName: devInfo,
strategy: Strategy.P2P_CLUSTER,
callback: (isRunning) async {
if (isRunning) {
Expand All @@ -302,7 +312,7 @@ class _DevicesListScreenState extends State<DevicesListScreen> {
});
subscription =
nearbyService.stateChangedSubscription(callback: (devicesList) {
devicesList?.forEach((element) {
devicesList.forEach((element) {
print(
" deviceId: ${element.deviceId} | deviceName: ${element.deviceName} | state: ${element.state}");

Expand All @@ -328,7 +338,11 @@ class _DevicesListScreenState extends State<DevicesListScreen> {
receivedDataSubscription =
nearbyService.dataReceivedSubscription(callback: (data) {
print("dataReceivedSubscription: ${jsonEncode(data)}");
Fluttertoast.showToast(msg: jsonEncode(data));
showToast(jsonEncode(data),
context: context,
axis: Axis.horizontal,
alignment: Alignment.center,
position: StyledToastPosition.bottom);
});
}
}
10 changes: 5 additions & 5 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ description: Demonstrates how to use the flutter_nearby_connections plugin.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev

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

dependencies:
flutter:
sdk: flutter

fluttertoast: ^7.0.4
flutter_styled_toast: ^2.0.0

flutter_nearby_connections:
path: ../

cupertino_icons: ^0.1.3
device_info: ^0.4.0+4
flutter_udid: ^1.0.1
cupertino_icons: ^1.0.2
device_info: ^2.0.0
flutter_udid: ^2.0.0

dev_dependencies:
flutter_test:
Expand Down
2 changes: 1 addition & 1 deletion example/test/widget_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void main() {
expect(
find.byWidgetPredicate(
(Widget widget) =>
widget is Text && widget.data.startsWith('Running on:'),
widget is Text && widget.data!.startsWith('Running on:'),
),
findsOneWidget,
);
Expand Down
2 changes: 2 additions & 0 deletions lib/flutter_nearby_connections.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ library flutter_nearby_connections;

import 'dart:async';
import 'dart:convert';
import 'dart:core';

import 'dart:io';

import 'package:flutter/foundation.dart';
Expand Down
27 changes: 15 additions & 12 deletions lib/src/nearby_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ class NearbyService {
/// param [deviceId] is unique, you should use the UDID for [deviceId]
/// param [strategy] Nearby Connections supports different Strategies for advertising and discovery. The best Strategy to use depends on the use case. only support android OS
Future init(
{@required String serviceType,
@required Strategy strategy,
String deviceName,
@required Function callback}) async {
assert(serviceType.length <= 15 &&
serviceType != null &&
{@required String? serviceType,
@required Strategy? strategy,
String? deviceName,
@required Function? callback}) async {
assert(serviceType!.length <= 15 &&
//serviceType != null &&
serviceType.isNotEmpty);

_channel.setMethodCallHandler((handler) async {
Expand All @@ -72,7 +72,7 @@ class NearbyService {
break;
case _invokeNearbyRunning:
await Future.delayed(Duration(seconds: 1));
callback(handler.arguments as bool);
callback!(handler.arguments as bool);
break;
}
});
Expand All @@ -91,6 +91,9 @@ class NearbyService {
case Strategy.Wi_Fi_P2P:
strategyValue = 0;
break;
case null :
strategyValue = -1; //error
break;
}

_channel.invokeMethod(
Expand All @@ -103,7 +106,7 @@ class NearbyService {
);
if (Platform.isIOS) {
await Future.delayed(Duration(seconds: 1));
callback(true);
callback!(true);
}
}

Expand Down Expand Up @@ -137,7 +140,7 @@ class NearbyService {
/// Invites a discovered peer to join a nearby connections session.
/// the [deviceID] is current Device
FutureOr<dynamic> invitePeer(
{@required String deviceID, @required String deviceName}) async {
{@required String? deviceID, @required String? deviceName}) async {
await _channel.invokeMethod(
_invitePeer,
<String, dynamic>{
Expand All @@ -149,7 +152,7 @@ class NearbyService {

/// Disconnects the local peer from the session.
/// the [deviceID] is current Device
FutureOr<dynamic> disconnectPeer({@required String deviceID}) async {
FutureOr<dynamic> disconnectPeer({@required String? deviceID}) async {
await _channel.invokeMethod(_disconnectPeer, <String, dynamic>{
'deviceId': deviceID,
});
Expand All @@ -169,14 +172,14 @@ class NearbyService {
/// [stateChangedSubscription] will return you a list of [Device].
/// see [StateChangedCallback]
StreamSubscription stateChangedSubscription(
{@required StateChangedCallback callback}) =>
{@required StateChangedCallback? callback}) =>
_stateChangedStream.listen(callback);

/// The [dataReceivedSubscription] helps you listen when a peer sends you
/// text messages. and it returns you a object [Data].
/// It returns a [StreamSubscription] so you can cancel listening at any time.
/// see [DataReceivedCallback]
StreamSubscription dataReceivedSubscription(
{@required DataReceivedCallback callback}) =>
{@required DataReceivedCallback? callback}) =>
_dataReceivedStream.listen(callback);
}
6 changes: 3 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ author: [email protected]
homepage: https://github.com/VNAPNIC/flutter_nearby_connections

environment:
sdk: ">=2.7.0 <3.0.0"
sdk: '>=2.12.0 <3.0.0'
flutter: ">=1.10.0"

dependencies:
flutter:
sdk: flutter
pedantic: ^1.9.0
fluttertoast: ^7.1.1



dev_dependencies:
flutter_test:
Expand Down

0 comments on commit 65676ca

Please sign in to comment.