Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 5 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
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
20 changes: 10 additions & 10 deletions packages/sensors/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class MyApp extends StatelessWidget {
}

class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
MyHomePage({Key? key, required this.title}) : super(key: key);

final String title;

Expand All @@ -41,21 +41,21 @@ class _MyHomePageState extends State<MyHomePage> {
static const int _snakeColumns = 20;
static const double _snakeCellSize = 10.0;

List<double> _accelerometerValues;
List<double> _userAccelerometerValues;
List<double> _gyroscopeValues;
List<double>? _accelerometerValues;
List<double>? _userAccelerometerValues;
List<double>? _gyroscopeValues;
List<StreamSubscription<dynamic>> _streamSubscriptions =
<StreamSubscription<dynamic>>[];

@override
Widget build(BuildContext context) {
final List<String> accelerometer =
_accelerometerValues?.map((double v) => v.toStringAsFixed(1))?.toList();
final List<String> gyroscope =
_gyroscopeValues?.map((double v) => v.toStringAsFixed(1))?.toList();
final List<String> userAccelerometer = _userAccelerometerValues
final List<String>? accelerometer =
_accelerometerValues?.map((double v) => v.toStringAsFixed(1)).toList();
final List<String>? gyroscope =
_gyroscopeValues?.map((double v) => v.toStringAsFixed(1)).toList();
final List<String>? userAccelerometer = _userAccelerometerValues
?.map((double v) => v.toStringAsFixed(1))
?.toList();
.toList();

return Scaffold(
appBar: AppBar(
Expand Down
23 changes: 12 additions & 11 deletions packages/sensors/example/lib/snake.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ class SnakeState extends State<Snake> {
}

double cellSize;
GameState state;
AccelerometerEvent acceleration;
StreamSubscription<AccelerometerEvent> _streamSubscription;
Timer _timer;
late final GameState state;
AccelerometerEvent? acceleration;
late final StreamSubscription<AccelerometerEvent> _streamSubscription;
late final Timer _timer;

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -96,13 +96,14 @@ class SnakeState extends State<Snake> {
}

void _step() {
final math.Point<int> newDirection = acceleration == null
final math.Point<int>? newDirection = acceleration == null
? null
: acceleration.x.abs() < 1.0 && acceleration.y.abs() < 1.0
: acceleration!.x.abs() < 1.0 && acceleration!.y.abs() < 1.0
? null
: (acceleration.x.abs() < acceleration.y.abs())
? math.Point<int>(0, acceleration.y.sign.toInt())
: math.Point<int>(-acceleration.x.sign.toInt(), 0);
: (acceleration!.x.abs() < acceleration!.y.abs())
? math.Point<int>(0, acceleration!.y.sign.toInt())
: math.Point<int>(-acceleration!.x.sign.toInt(), 0);

state.step(newDirection);
}
}
Expand All @@ -114,12 +115,12 @@ class GameState {

int rows;
int columns;
int snakeLength;
late int snakeLength;

List<math.Point<int>> body = <math.Point<int>>[const math.Point<int>(0, 0)];
math.Point<int> direction = const math.Point<int>(1, 0);

void step(math.Point<int> newDirection) {
void step(math.Point<int>? newDirection) {
math.Point<int> next = body.last + direction;
next = math.Point<int>(next.x % columns, next.y % rows);

Expand Down
4 changes: 2 additions & 2 deletions packages/sensors/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ dev_dependencies:
sdk: flutter
integration_test:
path: ../../integration_test
pedantic: ^1.8.0
pedantic: ^1.10.0-nullsafety

flutter:
uses-material-design: true

environment:
sdk: ">=2.0.0-dev.28.0 <3.0.0"
sdk: ">=2.12.0-0 <3.0.0"
flutter: ">=1.9.1+hotfix.2 <2.0.0"

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 'dart:convert';
import 'dart:io';
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 @@ -14,6 +14,7 @@ readonly NNBD_PLUGINS_LIST=(
"local_auth"
"path_provider"
"plugin_platform_interface"
"sensors"
"share"
"url_launcher"
"video_player"
Expand Down