Skip to content

Commit

Permalink
[sensors] Migrate to null safety (flutter#3423)
Browse files Browse the repository at this point in the history
  • Loading branch information
bparrishMines authored and adsonpleal committed Feb 26, 2021
1 parent f12a374 commit b13938b
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 30 deletions.
4 changes: 4 additions & 0 deletions packages/sensors/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.5.0-nullsafety

* Migrate to null safety.

## 0.4.2+8

* Fix outdated links across a number of markdown files ([#3276](https://github.com/flutter/plugins/pull/3276))
Expand Down
2 changes: 2 additions & 0 deletions packages/sensors/integration_test/sensors_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// @dart = 2.9

import 'dart:async';
import 'package:flutter_test/flutter_test.dart';
import 'package:sensors/sensors.dart';
Expand Down
52 changes: 32 additions & 20 deletions packages/sensors/lib/sensors.dart
Original file line number Diff line number Diff line change
Expand Up @@ -121,38 +121,50 @@ GyroscopeEvent _listToGyroscopeEvent(List<double> list) {
return GyroscopeEvent(list[0], list[1], list[2]);
}

Stream<AccelerometerEvent> _accelerometerEvents;
Stream<GyroscopeEvent> _gyroscopeEvents;
Stream<UserAccelerometerEvent> _userAccelerometerEvents;
Stream<AccelerometerEvent>? _accelerometerEvents;
Stream<GyroscopeEvent>? _gyroscopeEvents;
Stream<UserAccelerometerEvent>? _userAccelerometerEvents;

/// A broadcast stream of events from the device accelerometer.
Stream<AccelerometerEvent> get accelerometerEvents {
if (_accelerometerEvents == null) {
_accelerometerEvents = _accelerometerEventChannel
.receiveBroadcastStream()
.map(
(dynamic event) => _listToAccelerometerEvent(event.cast<double>()));
Stream<AccelerometerEvent>? accelerometerEvents = _accelerometerEvents;
if (accelerometerEvents == null) {
accelerometerEvents =
_accelerometerEventChannel.receiveBroadcastStream().map(
(dynamic event) =>
_listToAccelerometerEvent(event.cast<double>()),
);
_accelerometerEvents = accelerometerEvents;
}
return _accelerometerEvents;

return accelerometerEvents;
}

/// A broadcast stream of events from the device gyroscope.
Stream<GyroscopeEvent> get gyroscopeEvents {
if (_gyroscopeEvents == null) {
_gyroscopeEvents = _gyroscopeEventChannel
.receiveBroadcastStream()
.map((dynamic event) => _listToGyroscopeEvent(event.cast<double>()));
Stream<GyroscopeEvent>? gyroscopeEvents = _gyroscopeEvents;
if (gyroscopeEvents == null) {
gyroscopeEvents = _gyroscopeEventChannel.receiveBroadcastStream().map(
(dynamic event) => _listToGyroscopeEvent(event.cast<double>()),
);
_gyroscopeEvents = gyroscopeEvents;
}
return _gyroscopeEvents;

return gyroscopeEvents;
}

/// Events from the device accelerometer with gravity removed.
Stream<UserAccelerometerEvent> get userAccelerometerEvents {
if (_userAccelerometerEvents == null) {
_userAccelerometerEvents = _userAccelerometerEventChannel
.receiveBroadcastStream()
.map((dynamic event) =>
_listToUserAccelerometerEvent(event.cast<double>()));
Stream<UserAccelerometerEvent>? userAccelerometerEvents =
_userAccelerometerEvents;
if (userAccelerometerEvents == null) {
userAccelerometerEvents =
_userAccelerometerEventChannel.receiveBroadcastStream().map(
(dynamic event) =>
_listToUserAccelerometerEvent(event.cast<double>()),
);
_userAccelerometerEvents = userAccelerometerEvents;
}
return _userAccelerometerEvents;

return userAccelerometerEvents;
}
10 changes: 5 additions & 5 deletions packages/sensors/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ homepage: https://github.com/flutter/plugins/tree/master/packages/sensors
# 0.4.y+z is compatible with 1.0.0, if you land a breaking change bump
# the version to 2.0.0.
# See more details: https://github.com/flutter/flutter/wiki/Package-migration-to-1.0.0
version: 0.4.2+8
version: 0.5.0-nullsafety

flutter:
plugin:
Expand All @@ -21,14 +21,14 @@ dependencies:
sdk: flutter

dev_dependencies:
test: ^1.3.0
test: ^1.16.0-nullsafety
flutter_test:
sdk: flutter
integration_test:
path: ../integration_test
mockito: ^4.1.1
pedantic: ^1.8.0
mockito: ^5.0.0-nullsafety.0
pedantic: ^1.10.0-nullsafety

environment:
sdk: ">=2.1.0<3.0.0"
sdk: ">=2.12.0-0 <3.0.0"
flutter: ">=1.12.13+hotfix.5"
10 changes: 5 additions & 5 deletions packages/sensors/test/sensors_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,16 @@ void main() {
void _initializeFakeSensorChannel(String channelName, List<double> sensorData) {
const StandardMethodCodec standardMethod = StandardMethodCodec();

void _emitEvent(ByteData event) {
ServicesBinding.instance.defaultBinaryMessenger.handlePlatformMessage(
void _emitEvent(ByteData? event) {
ServicesBinding.instance!.defaultBinaryMessenger.handlePlatformMessage(
channelName,
event,
(ByteData reply) {},
(ByteData? reply) {},
);
}

ServicesBinding.instance.defaultBinaryMessenger
.setMockMessageHandler(channelName, (ByteData message) async {
ServicesBinding.instance!.defaultBinaryMessenger
.setMockMessageHandler(channelName, (ByteData? message) async {
final MethodCall methodCall = standardMethod.decodeMethodCall(message);
if (methodCall.method == 'listen') {
_emitEvent(standardMethod.encodeSuccessEnvelope(sensorData));
Expand Down
1 change: 1 addition & 0 deletions script/nnbd_plugins.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ readonly NNBD_PLUGINS_LIST=(
"path_provider"
"package_info"
"plugin_platform_interface"
"sensors"
"share"
"shared_preferences"
"url_launcher"
Expand Down

0 comments on commit b13938b

Please sign in to comment.