Skip to content

Commit

Permalink
[battery] Migrate battery_plugin_interface to null safety (flutter#3366)
Browse files Browse the repository at this point in the history
  • Loading branch information
bparrishMines authored Jan 8, 2021
1 parent d01c84c commit b3bc1f2
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 15 deletions.
4 changes: 4 additions & 0 deletions packages/battery/battery_platform_interface/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.0.0-nullsafety

* Migrate to null safety.

## 1.0.1

- Update Flutter SDK constraint.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ import '../battery_platform_interface.dart';
class MethodChannelBattery extends BatteryPlatform {
/// The method channel used to interact with the native platform.
@visibleForTesting
MethodChannel channel = MethodChannel('plugins.flutter.io/battery');
final MethodChannel channel = MethodChannel('plugins.flutter.io/battery');

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

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

/// Stream variable for storing battery state.
Stream<BatteryState> _onBatteryStateChanged;
Stream<BatteryState>? _onBatteryStateChanged;

/// Event channel for getting battery change state.
Stream<BatteryState> onBatteryStateChanged() {
Expand All @@ -32,7 +32,8 @@ class MethodChannelBattery extends BatteryPlatform {
.receiveBroadcastStream()
.map((dynamic event) => _parseBatteryState(event));
}
return _onBatteryStateChanged;

return _onBatteryStateChanged!;
}
}

Expand Down
12 changes: 6 additions & 6 deletions packages/battery/battery_platform_interface/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ description: A common platform interface for the battery plugin.
homepage: https://github.com/flutter/plugins/tree/master/packages/battery
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
version: 1.0.1
version: 2.0.0-nullsafety

dependencies:
flutter:
sdk: flutter
meta: ^1.1.8
plugin_platform_interface: ^1.0.2
meta: ^1.3.0-nullsafety
plugin_platform_interface: ^1.1.0-nullsafety.1

dev_dependencies:
flutter_test:
sdk: flutter
mockito: ^4.1.1
pedantic: ^1.8.0
mockito: ^5.0.0-nullsafety.0
pedantic: ^1.10.0-nullsafety

environment:
sdk: ">=2.7.0 <3.0.0"
sdk: ">=2.12.0-0 <3.0.0"
flutter: ">=1.9.1+hotfix.4"
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import 'package:battery_platform_interface/method_channel/method_channel_battery
void main() {
TestWidgetsFlutterBinding.ensureInitialized();

group("$MethodChannelBattery", () {
MethodChannelBattery methodChannelBattery;
group('$MethodChannelBattery', () {
late MethodChannelBattery methodChannelBattery;

setUp(() async {
methodChannelBattery = MethodChannelBattery();
Expand All @@ -32,7 +32,7 @@ void main() {
.setMockMethodCallHandler((MethodCall methodCall) async {
switch (methodCall.method) {
case 'listen':
await ServicesBinding.instance.defaultBinaryMessenger
await ServicesBinding.instance!.defaultBinaryMessenger
.handlePlatformMessage(
methodChannelBattery.eventChannel.name,
methodChannelBattery.eventChannel.codec
Expand All @@ -48,13 +48,13 @@ void main() {
});

/// Test for batetry level call.
test("getBatteryLevel", () async {
test('getBatteryLevel', () async {
final int result = await methodChannelBattery.batteryLevel();
expect(result, 90);
});

/// Test for battery changed state call.
test("onBatteryChanged", () async {
test('onBatteryChanged', () async {
final BatteryState result =
await methodChannelBattery.onBatteryStateChanged().first;
expect(result, BatteryState.full);
Expand Down
1 change: 1 addition & 0 deletions script/nnbd_plugins.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

readonly NNBD_PLUGINS_LIST=(
"android_intent"
"battery"
"connectivity"
"device_info"
"flutter_plugin_android_lifecycle"
Expand Down

0 comments on commit b3bc1f2

Please sign in to comment.