Skip to content

Commit be709e7

Browse files
stuartmorgan-gadsonpleal
authored andcommitted
[sensors] Update to NNBD stable (flutter#3589)
Includes migrating example to null-safety.
1 parent d589e76 commit be709e7

File tree

6 files changed

+34
-38
lines changed

6 files changed

+34
-38
lines changed

packages/sensors/CHANGELOG.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
## 2.0.0-nullsafety
2-
3-
* * Update version to (semi-belatedly) meet 1.0-consistency promise.
4-
5-
## 0.5.0-nullsafety
1+
## 2.0.0
62

73
* Migrate to null safety.
84

packages/sensors/example/lib/main.dart

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class MyApp extends StatelessWidget {
2828
}
2929

3030
class MyHomePage extends StatefulWidget {
31-
MyHomePage({Key key, this.title}) : super(key: key);
31+
MyHomePage({Key? key, required this.title}) : super(key: key);
3232

3333
final String title;
3434

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

44-
List<double> _accelerometerValues;
45-
List<double> _userAccelerometerValues;
46-
List<double> _gyroscopeValues;
44+
List<double>? _accelerometerValues;
45+
List<double>? _userAccelerometerValues;
46+
List<double>? _gyroscopeValues;
4747
List<StreamSubscription<dynamic>> _streamSubscriptions =
4848
<StreamSubscription<dynamic>>[];
4949

5050
@override
5151
Widget build(BuildContext context) {
52-
final List<String> accelerometer =
53-
_accelerometerValues?.map((double v) => v.toStringAsFixed(1))?.toList();
54-
final List<String> gyroscope =
55-
_gyroscopeValues?.map((double v) => v.toStringAsFixed(1))?.toList();
56-
final List<String> userAccelerometer = _userAccelerometerValues
52+
final List<String>? accelerometer =
53+
_accelerometerValues?.map((double v) => v.toStringAsFixed(1)).toList();
54+
final List<String>? gyroscope =
55+
_gyroscopeValues?.map((double v) => v.toStringAsFixed(1)).toList();
56+
final List<String>? userAccelerometer = _userAccelerometerValues
5757
?.map((double v) => v.toStringAsFixed(1))
58-
?.toList();
58+
.toList();
5959

6060
return Scaffold(
6161
appBar: AppBar(

packages/sensors/example/lib/snake.dart

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,14 @@ class SnakeBoardPainter extends CustomPainter {
5656
}
5757

5858
class SnakeState extends State<Snake> {
59-
SnakeState(int rows, int columns, this.cellSize) {
60-
state = GameState(rows, columns);
61-
}
59+
SnakeState(int rows, int columns, this.cellSize)
60+
: state = GameState(rows, columns);
6261

6362
double cellSize;
6463
GameState state;
65-
AccelerometerEvent acceleration;
66-
StreamSubscription<AccelerometerEvent> _streamSubscription;
67-
Timer _timer;
64+
AccelerometerEvent? acceleration;
65+
late StreamSubscription<AccelerometerEvent> _streamSubscription;
66+
late Timer _timer;
6867

6968
@override
7069
Widget build(BuildContext context) {
@@ -96,21 +95,21 @@ class SnakeState extends State<Snake> {
9695
}
9796

9897
void _step() {
99-
final math.Point<int> newDirection = acceleration == null
98+
final AccelerometerEvent? currentAcceleration = acceleration;
99+
final math.Point<int>? newDirection = currentAcceleration == null
100100
? null
101-
: acceleration.x.abs() < 1.0 && acceleration.y.abs() < 1.0
101+
: currentAcceleration.x.abs() < 1.0 && currentAcceleration.y.abs() < 1.0
102102
? null
103-
: (acceleration.x.abs() < acceleration.y.abs())
104-
? math.Point<int>(0, acceleration.y.sign.toInt())
105-
: math.Point<int>(-acceleration.x.sign.toInt(), 0);
103+
: (currentAcceleration.x.abs() < currentAcceleration.y.abs())
104+
? math.Point<int>(0, currentAcceleration.y.sign.toInt())
105+
: math.Point<int>(-currentAcceleration.x.sign.toInt(), 0);
106106
state.step(newDirection);
107107
}
108108
}
109109

110110
class GameState {
111-
GameState(this.rows, this.columns) {
112-
snakeLength = math.min(rows, columns) - 5;
113-
}
111+
GameState(this.rows, this.columns)
112+
: snakeLength = math.min(rows, columns) - 5;
114113

115114
int rows;
116115
int columns;
@@ -119,7 +118,7 @@ class GameState {
119118
List<math.Point<int>> body = <math.Point<int>>[const math.Point<int>(0, 0)];
120119
math.Point<int> direction = const math.Point<int>(1, 0);
121120

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

packages/sensors/example/pubspec.yaml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@ dev_dependencies:
1717
sdk: flutter
1818
integration_test:
1919
path: ../../integration_test
20-
pedantic: ^1.8.0
20+
pedantic: ^1.10.0
2121

2222
flutter:
2323
uses-material-design: true
2424

2525
environment:
26-
sdk: ">=2.0.0-dev.28.0 <3.0.0"
27-
flutter: ">=1.9.1+hotfix.2"
28-
26+
sdk: ">=2.12.0-259.9.beta <3.0.0"
27+
flutter: ">=1.20.0"

packages/sensors/example/test_driver/test/integration_test.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
// @dart = 2.9
6+
57
import 'dart:async';
68
import 'dart:convert';
79
import 'dart:io';

packages/sensors/pubspec.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: sensors
22
description: Flutter plugin for accessing the Android and iOS accelerometer and
33
gyroscope sensors.
44
homepage: https://github.com/flutter/plugins/tree/master/packages/sensors
5-
version: 2.0.0-nullsafety
5+
version: 2.0.0
66

77
flutter:
88
plugin:
@@ -18,14 +18,14 @@ dependencies:
1818
sdk: flutter
1919

2020
dev_dependencies:
21-
test: ^1.16.0-nullsafety
21+
test: ^1.16.0
2222
flutter_test:
2323
sdk: flutter
2424
integration_test:
2525
path: ../integration_test
2626
mockito: ^5.0.0-nullsafety.0
27-
pedantic: ^1.10.0-nullsafety
27+
pedantic: ^1.10.0
2828

2929
environment:
30-
sdk: ">=2.12.0-0 <3.0.0"
30+
sdk: ">=2.12.0-259.9.beta <3.0.0"
3131
flutter: ">=1.12.13+hotfix.5"

0 commit comments

Comments
 (0)