creativecreatorormaybenot
Committed by GitHub

Migrate to null safety (#62)

* Migrate wakelock_web

* Adjust constraint

* flutter constraint

* Finish

* Temporarily unsound
... ... @@ -96,4 +96,4 @@ jobs:
if: matrix.package == 'wakelock'
run: |
cd wakelock/example
flutter drive --driver=test_driver/integration_test.dart --target=integration_test/wakelock_test.dart
flutter drive --driver=test_driver/integration_test.dart --target=integration_test/wakelock_test.dart --no-sound-null-safety
... ...
## 0.3.0-nullsafety.0
* Migrated to null safety.
* **Breaking**: removed deprecated `isEnabled` and `on` in `toggle`.
## 0.2.1+1
* Fixed Android build warning (`import_js_library`) by bumping `wakelock_web`.
... ...
import 'package:flutter_test/flutter_test.dart';
// See https://github.com/flutter/flutter/issues/71379.
// ignore: import_of_legacy_library_into_null_safe
import 'package:integration_test/integration_test.dart';
import 'package:wakelock/wakelock.dart';
... ...
... ... @@ -58,16 +58,17 @@ class _WakelockExampleAppState extends State<WakelockExampleApp> {
FutureBuilder(
future: Wakelock.enabled,
builder: (context, AsyncSnapshot<bool> snapshot) {
final data = snapshot.data;
// The use of FutureBuilder is necessary here to await the
// bool value from the `enabled` getter.
if (!snapshot.hasData) {
if (data == null) {
// The Future is retrieved so fast that you will not be able
// to see any loading indicator.
return Container();
}
return Text('The wakelock is currently '
'${snapshot.data ? 'enabled' : 'disabled'}.');
'${data ? 'enabled' : 'disabled'}.');
},
),
const Spacer(
... ...
... ... @@ -5,7 +5,7 @@ description: >-2
publish_to: 'none'
environment:
sdk: ">=2.7.0 <3.0.0"
sdk: '>=2.12.0-0 <3.0.0'
dependencies:
flutter:
... ... @@ -24,7 +24,8 @@ dev_dependencies:
sdk: flutter
flutter_driver:
sdk: flutter
integration_test: ^0.9.1
integration_test: ^0.9.2+2
test: any
pedantic: ^1.9.2
pedantic: ^1.10.0-nullsafety.3
... ...
// See https://github.com/flutter/flutter/issues/71379.
// ignore: import_of_legacy_library_into_null_safe
import 'package:integration_test/integration_test_driver.dart';
Future<void> main() => integrationDriver();
... ...
import 'package:meta/meta.dart';
import 'package:wakelock_platform_interface/wakelock_platform_interface.dart';
final _wakelockPlatformInstance = WakelockPlatformInterface.instance;
... ... @@ -53,20 +52,9 @@ class Wakelock {
///
/// You can await the [Future] to wait for the operation to complete.
static Future<void> toggle({
@required bool enable,
@Deprecated('Use the `enable` parameter instead.') bool on,
required bool enable,
}) {
// The checks allow only `on` to be used in the case of old code and
// they encourage to use only `enable` instead (combined with the
// deprecation warning).
assert(enable != null || on != null,
'The `enable` parameter must not be null when toggling the wakelock.');
assert(
on == null || enable == null,
'The `on` parameter has been deprecated; '
'specify only the `enable` parameter instead.');
return _wakelockPlatformInstance.toggle(enable: enable ?? on);
return _wakelockPlatformInstance.toggle(enable: enable);
}
/// Returns whether the wakelock is currently enabled or not.
... ... @@ -78,8 +66,4 @@ class Wakelock {
/// bool wakelockEnabled = await Wakelock.enabled;
/// ```
static Future<bool> get enabled => _wakelockPlatformInstance.enabled;
/// Returns the current wakelock status.
@Deprecated('Use the `enabled` getter instead.')
static Future<bool> get isEnabled => enabled;
}
... ...
// This is fine because it is a dev dependency.
// See https://github.com/flutter/flutter/issues/71360.
// @dart=2.9
import 'package:pigeon/pigeon.dart';
/// Message for toggling the wakelock on the platform side.
... ...
... ... @@ -2,12 +2,12 @@ name: wakelock
description: >-2
Plugin that allows you to keep the device screen awake, i.e. prevent the screen from sleeping on
Android, iOS, and web.
version: 0.2.1+1
version: 0.3.0-nullsafety.0
homepage: https://github.com/creativecreatorormaybenot/wakelock/tree/master/wakelock
environment:
sdk: ">=2.8.0 <3.0.0"
flutter: ">=1.20.0 <2.0.0"
sdk: '>=2.12.0-0 <3.0.0'
flutter: '>=1.24.0-0 <2.0.0'
dependencies:
flutter:
... ... @@ -15,15 +15,15 @@ dependencies:
meta: ^1.2.0
wakelock_platform_interface: ^0.1.0+1
wakelock_web: ^0.1.0+3
wakelock_platform_interface: ^0.2.0-nullsafety.2
wakelock_web: ^0.2.0-nullsafety.0
dev_dependencies:
flutter_test:
sdk: flutter
pedantic: ^1.9.2
pigeon: ^0.1.14
pedantic: ^1.10.0-nullsafety.0
pigeon: ^0.1.15
flutter:
plugin:
... ...
... ... @@ -6,16 +6,12 @@ void main() {
TestWidgetsFlutterBinding.ensureInitialized();
group('$Wakelock', () {
FakeWakelockApi fakeWakelock;
late FakeWakelockApi fakeWakelock;
setUpAll(() {
fakeWakelock = FakeWakelockApi();
});
test('toggle enable non-null assertion', () {
expect(() => Wakelock.toggle(enable: null), throwsAssertionError);
});
test('enable', () async {
await Wakelock.enable();
... ... @@ -55,7 +51,7 @@ class FakeWakelockApi extends TestWakelockApi {
}
final calls = <String>[];
ToggleMessage toggleMessage;
late ToggleMessage toggleMessage;
@override
IsEnabledMessage isEnabled() {
... ...
## 0.2.0-nullsafety.0
* Migrated to null safety.
* Temporarily removed `import_js_library` dependency and included code internally.
## 0.1.0+3
* Bumped minimum `import_js_library` version in order to resolve Android build warning.
... ...
/// Function with a single typed argument.
typedef Func1<A, R> = R Function(A a);
... ...
import 'dart:html' as html;
/// This is an implementation of the `import_js_library` plugin that is used
/// until that plugin is migrated to null safety.
/// See https://github.com/florent37/flutter_web_import_js_library/pull/6#issuecomment-735349208.
/// Imports a JS script file from the given [url] given the relative
/// [flutterPluginName].
void importJsLibrary({required String url, String? flutterPluginName}) {
if (flutterPluginName == null) {
_importJSLibraries([url]);
} else {
_importJSLibraries([_libraryUrl(url, flutterPluginName)]);
}
}
String _libraryUrl(String url, String pluginName) {
if (url.startsWith('./')) {
url = url.replaceFirst('./', '');
return './assets/packages/$pluginName/$url';
}
if (url.startsWith('assets/')) {
return './assets/packages/$pluginName/$url';
} else {
return url;
}
}
html.ScriptElement _createScriptTag(String library) {
final script = html.ScriptElement()
..type = 'text/javascript'
..charset = 'utf-8'
..async = true
..src = library;
return script;
}
/// Injects a bunch of libraries in the <head> and returns a
/// Future that resolves when all load.
Future<void> _importJSLibraries(List<String> libraries) {
final loading = <Future<void>>[];
final head = html.querySelector('head');
libraries.forEach((String library) {
if (!_isImported(library)) {
final scriptTag = _createScriptTag(library);
head!.children.add(scriptTag);
loading.add(scriptTag.onLoad.first);
}
});
return Future.wait(loading);
}
bool _isImported(String url) {
final head = html.querySelector('head')!;
return _isLoaded(head, url);
}
bool _isLoaded(html.Element head, String url) {
if (url.startsWith('./')) {
url = url.replaceFirst('./', '');
}
for (var element in head.children) {
if (element is html.ScriptElement) {
if (element.src.endsWith(url)) {
return true;
}
}
}
return false;
}
... ...
... ... @@ -5,7 +5,7 @@ import 'package:js/js.dart';
import 'package:wakelock_web/src/promise.dart';
/// Toggles the JS wakelock.
external toggle(bool enable);
external void toggle(bool enable);
/// Returns a JS promise of whether the wakelock is enabled or not.
external PromiseJsImpl<bool> enabled();
... ...
... ... @@ -11,5 +11,5 @@ class PromiseJsImpl<T> {
external PromiseJsImpl(Function resolver);
/// Attaches callbacks to a JS promise.
external PromiseJsImpl then([Func1 onResolve, Func1 onReject]);
external PromiseJsImpl then([Func1? onResolve, Func1? onReject]);
}
... ...
import 'dart:async';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
import 'package:import_js_library/import_js_library.dart';
import 'package:js/js.dart';
import 'package:wakelock_platform_interface/wakelock_platform_interface.dart';
import 'package:wakelock_web/src/js_wakelock.dart' as Wakelock;
import 'package:wakelock_web/src/import_js_library.dart';
import 'package:wakelock_web/src/js_wakelock.dart' as wakelock_web;
/// The web implementation of the [WakelockPlatformInterface].
///
... ... @@ -22,17 +22,15 @@ class WakelockWeb extends WakelockPlatformInterface {
}
@override
Future<void> toggle({bool enable}) async {
assert(enable != null);
Wakelock.toggle(enable);
Future<void> toggle({required bool enable}) async {
wakelock_web.toggle(enable);
}
@override
Future<bool> get enabled async {
final completer = Completer<bool>();
Wakelock.enabled().then(
wakelock_web.enabled().then(
// onResolve
allowInterop((value) {
assert(value is bool);
... ...
name: wakelock_web
description: Web platform implementation of the wakelock_platform_interface for the wakelock plugin.
version: 0.1.0+3
version: 0.2.0-nullsafety.0
homepage: https://github.com/creativecreatorormaybenot/wakelock/tree/master/wakelock_web
environment:
sdk: ">=2.8.0 <3.0.0"
flutter: ">=1.20.0 <2.0.0"
sdk: '>=2.12.0-0 <3.0.0'
flutter: '>=1.24.0-0 <2.0.0'
dependencies:
flutter:
... ... @@ -13,16 +13,15 @@ dependencies:
flutter_web_plugins:
sdk: flutter
import_js_library: ^1.0.2
js: ^0.6.2
js: ^0.6.3-nullsafety.0
wakelock_platform_interface: ^0.1.0
wakelock_platform_interface: ^0.2.0-nullsafety.2
dev_dependencies:
flutter_test:
sdk: flutter
pedantic: ^1.9.2
pedantic: ^1.10.0-nullsafety.0
wakelock:
path: ../wakelock
... ...