Skip to content

Commit 8200051

Browse files
bparrishMinesMinyewoo
authored andcommitted
[battery] Migrate battery_plugin_interface to null safety (flutter#3366)
1 parent 3c85d4a commit 8200051

File tree

5 files changed

+21
-15
lines changed

5 files changed

+21
-15
lines changed

packages/battery/battery_platform_interface/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.0.0-nullsafety
2+
3+
* Migrate to null safety.
4+
15
## 1.0.1
26

37
- Update Flutter SDK constraint.

packages/battery/battery_platform_interface/lib/method_channel/method_channel_battery.dart

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@ import '../battery_platform_interface.dart';
1111
class MethodChannelBattery extends BatteryPlatform {
1212
/// The method channel used to interact with the native platform.
1313
@visibleForTesting
14-
MethodChannel channel = MethodChannel('plugins.flutter.io/battery');
14+
final MethodChannel channel = MethodChannel('plugins.flutter.io/battery');
1515

1616
/// The event channel used to interact with the native platform.
1717
@visibleForTesting
18-
EventChannel eventChannel = EventChannel('plugins.flutter.io/charging');
18+
final EventChannel eventChannel = EventChannel('plugins.flutter.io/charging');
1919

2020
/// Method channel for getting battery level.
2121
Future<int> batteryLevel() async {
2222
return (await channel.invokeMethod('getBatteryLevel')).toInt();
2323
}
2424

2525
/// Stream variable for storing battery state.
26-
Stream<BatteryState> _onBatteryStateChanged;
26+
Stream<BatteryState>? _onBatteryStateChanged;
2727

2828
/// Event channel for getting battery change state.
2929
Stream<BatteryState> onBatteryStateChanged() {
@@ -32,7 +32,8 @@ class MethodChannelBattery extends BatteryPlatform {
3232
.receiveBroadcastStream()
3333
.map((dynamic event) => _parseBatteryState(event));
3434
}
35-
return _onBatteryStateChanged;
35+
36+
return _onBatteryStateChanged!;
3637
}
3738
}
3839

packages/battery/battery_platform_interface/pubspec.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@ description: A common platform interface for the battery plugin.
33
homepage: https://github.com/flutter/plugins/tree/master/packages/battery
44
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
55
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
6-
version: 1.0.1
6+
version: 2.0.0-nullsafety
77

88
dependencies:
99
flutter:
1010
sdk: flutter
11-
meta: ^1.1.8
12-
plugin_platform_interface: ^1.0.2
11+
meta: ^1.3.0-nullsafety
12+
plugin_platform_interface: ^1.1.0-nullsafety.1
1313

1414
dev_dependencies:
1515
flutter_test:
1616
sdk: flutter
17-
mockito: ^4.1.1
18-
pedantic: ^1.8.0
17+
mockito: ^5.0.0-nullsafety.0
18+
pedantic: ^1.10.0-nullsafety
1919

2020
environment:
21-
sdk: ">=2.7.0 <3.0.0"
21+
sdk: ">=2.12.0-0 <3.0.0"
2222
flutter: ">=1.9.1+hotfix.4"

packages/battery/battery_platform_interface/test/method_channel_battery_test.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import 'package:battery_platform_interface/method_channel/method_channel_battery
1212
void main() {
1313
TestWidgetsFlutterBinding.ensureInitialized();
1414

15-
group("$MethodChannelBattery", () {
16-
MethodChannelBattery methodChannelBattery;
15+
group('$MethodChannelBattery', () {
16+
late MethodChannelBattery methodChannelBattery;
1717

1818
setUp(() async {
1919
methodChannelBattery = MethodChannelBattery();
@@ -32,7 +32,7 @@ void main() {
3232
.setMockMethodCallHandler((MethodCall methodCall) async {
3333
switch (methodCall.method) {
3434
case 'listen':
35-
await ServicesBinding.instance.defaultBinaryMessenger
35+
await ServicesBinding.instance!.defaultBinaryMessenger
3636
.handlePlatformMessage(
3737
methodChannelBattery.eventChannel.name,
3838
methodChannelBattery.eventChannel.codec
@@ -48,13 +48,13 @@ void main() {
4848
});
4949

5050
/// Test for batetry level call.
51-
test("getBatteryLevel", () async {
51+
test('getBatteryLevel', () async {
5252
final int result = await methodChannelBattery.batteryLevel();
5353
expect(result, 90);
5454
});
5555

5656
/// Test for battery changed state call.
57-
test("onBatteryChanged", () async {
57+
test('onBatteryChanged', () async {
5858
final BatteryState result =
5959
await methodChannelBattery.onBatteryStateChanged().first;
6060
expect(result, BatteryState.full);

script/nnbd_plugins.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
readonly NNBD_PLUGINS_LIST=(
88
"android_intent"
9+
"battery"
910
"connectivity"
1011
"device_info"
1112
"flutter_plugin_android_lifecycle"

0 commit comments

Comments
 (0)