Skip to content

Commit f4051db

Browse files
Added pedantic lib for better static analysis (#494)
* Added pedantic lib for better static analysis Fixed (what I hope) are the biggest static analysis issues on pub.dev. * Fixed device_connection_mixin. * Code review updates. * Format fix.
1 parent cedaee1 commit f4051db

File tree

6 files changed

+48
-29
lines changed

6 files changed

+48
-29
lines changed

analysis_options.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include: package:pedantic/analysis_options.yaml

lib/ble_manager.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ abstract class BleManager {
3434
static BleManager _instance;
3535

3636
factory BleManager() {
37-
if (_instance == null) {
38-
_instance = InternalBleManager();
39-
}
37+
_instance ??= InternalBleManager();
4038

4139
return _instance;
4240
}

lib/peripheral.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ abstract class _PeripheralMetadata {
1515
/// [disconnectOrCancelConnection()] can be used if peripheral is not connected.
1616
class Peripheral {
1717
static const int NO_MTU_NEGOTIATION = 0;
18-
ManagerForPeripheral _manager;
18+
final ManagerForPeripheral _manager;
1919

2020
String name;
2121
String identifier;

lib/scan_result.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
part of flutter_ble_lib;
22

33
abstract class _ScanResultMetadata {
4-
static const String id = "id";
5-
static const String name = "name";
64
static const String rssi = "rssi";
75
static const String manufacturerData = "manufacturerData";
86
static const String serviceData = "serviceData";

lib/src/bridge/device_connection_mixin.dart

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,41 @@ mixin DeviceConnectionMixin on FlutterBLE {
77

88
Future<void> connectToPeripheral(String deviceIdentifier, bool isAutoConnect,
99
int requestMtu, bool refreshGatt, Duration timeout) async {
10-
return await _methodChannel
11-
.invokeMethod(MethodName.connectToDevice, <String, dynamic>{
12-
ArgumentName.deviceIdentifier: deviceIdentifier,
13-
ArgumentName.isAutoConnect: isAutoConnect,
14-
ArgumentName.requestMtu: requestMtu,
15-
ArgumentName.refreshGatt: refreshGatt,
16-
ArgumentName.timeoutMillis: timeout?.inMilliseconds
17-
}).catchError((errorJson) =>
18-
Future.error(BleError.fromJson(jsonDecode(errorJson.details))));
10+
return await _methodChannel.invokeMethod(
11+
MethodName.connectToDevice,
12+
<String, dynamic>{
13+
ArgumentName.deviceIdentifier: deviceIdentifier,
14+
ArgumentName.isAutoConnect: isAutoConnect,
15+
ArgumentName.requestMtu: requestMtu,
16+
ArgumentName.refreshGatt: refreshGatt,
17+
ArgumentName.timeoutMillis: timeout?.inMilliseconds
18+
},
19+
).catchError(
20+
(errorJson) => Future.error(
21+
BleError.fromJson(jsonDecode(errorJson.details)),
22+
),
23+
);
1924
}
2025

2126
Stream<PeripheralConnectionState> observePeripheralConnectionState(
22-
String identifier, bool emitCurrentValue) async* {
23-
yield* _peripheralConnectionStateChanges
27+
String identifier, bool emitCurrentValue) {
28+
var controller = StreamController<String>(
29+
onListen: () => _methodChannel.invokeMethod(
30+
MethodName.observeConnectionState,
31+
<String, dynamic>{
32+
ArgumentName.deviceIdentifier: identifier,
33+
ArgumentName.emitCurrentValue: emitCurrentValue,
34+
},
35+
).catchError(
36+
(errorJson) => throw BleError.fromJson(jsonDecode(errorJson.details)),
37+
),
38+
);
39+
40+
controller
41+
.addStream(_peripheralConnectionStateChanges)
42+
.then((value) => controller?.close());
43+
44+
return controller.stream
2445
.map((jsonString) =>
2546
ConnectionStateContainer.fromJson(jsonDecode(jsonString)))
2647
.where((connectionStateContainer) =>
@@ -39,32 +60,32 @@ mixin DeviceConnectionMixin on FlutterBLE {
3960
return PeripheralConnectionState.disconnecting;
4061
default:
4162
throw FormatException(
42-
"Unrecognized value of device connection state. Value: $connectionStateString");
63+
'Unrecognized value of device connection state. Value: $connectionStateString',
64+
);
4365
}
4466
});
45-
46-
_methodChannel.invokeMethod(
47-
MethodName.observeConnectionState, <String, dynamic>{
48-
ArgumentName.deviceIdentifier: identifier,
49-
ArgumentName.emitCurrentValue: emitCurrentValue,
50-
}).catchError(
51-
(errorJson) => throw BleError.fromJson(jsonDecode(errorJson.details)));
5267
}
5368

5469
Future<bool> isPeripheralConnected(String peripheralIdentifier) async {
5570
return await _methodChannel
5671
.invokeMethod(MethodName.isDeviceConnected, <String, dynamic>{
5772
ArgumentName.deviceIdentifier: peripheralIdentifier,
58-
}).catchError((errorJson) =>
59-
Future.error(BleError.fromJson(jsonDecode(errorJson.details))));
73+
}).catchError(
74+
(errorJson) => Future.error(
75+
BleError.fromJson(jsonDecode(errorJson.details)),
76+
),
77+
);
6078
}
6179

6280
Future<void> disconnectOrCancelPeripheralConnection(
6381
String peripheralIdentifier) async {
6482
return await _methodChannel
6583
.invokeMethod(MethodName.cancelConnection, <String, dynamic>{
6684
ArgumentName.deviceIdentifier: peripheralIdentifier,
67-
}).catchError((errorJson) =>
68-
Future.error(BleError.fromJson(jsonDecode(errorJson.details))));
85+
}).catchError(
86+
(errorJson) => Future.error(
87+
BleError.fromJson(jsonDecode(errorJson.details)),
88+
),
89+
);
6990
}
7091
}

pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ dependencies:
1717
dev_dependencies:
1818
test: ^1.5.3
1919
mockito: ^4.0.0
20+
pedantic: ^1.9.0
2021

2122
flutter_test:
2223
sdk: flutter

0 commit comments

Comments
 (0)