wakelock
wakelock copied to clipboard
Web - TypeError: Cannot read properties of undefined (reading 'toggle')
Calling Wakelock.enable()
within the main
function throws a TypeError
when running against web.
void main() {
WidgetsFlutterBinding.ensureInitialized();
Wakelock.enable();
runApp(const MyApp());
}
TypeError: Cannot read properties of undefined (reading 'toggle')
at toggle (http://localhost:52933/packages/wakelock_web/wakelock_web.dart.lib.js:35:30)
at toggle.next (<anonymous>)
at runBody (http://localhost:52933/dart_sdk.js:40660:34)
at Object._async [as async] (http://localhost:52933/dart_sdk.js:40691:7)
at wakelock_web.WakelockWeb.new.toggle (http://localhost:52933/packages/wakelock_web/wakelock_web.dart.lib.js:34:20)
at Function.toggle (http://localhost:52933/packages/wakelock/wakelock.dart.lib.js:26:48)
at Function.enable (http://localhost:52933/packages/wakelock/wakelock.dart.lib.js:19:32)
at main$ (http://localhost:52933/packages/flutter_application_1/main.dart.lib.js:285:23)
at http://localhost:52933/web_entrypoint.dart.lib.js:37:33
at Object._checkAndCall (http://localhost:52933/dart_sdk.js:5279:16)
at Object.dcall (http://localhost:52933/dart_sdk.js:5284:17)
at http://localhost:52933/dart_sdk.js:140291:18
This was replicated in a new project with only the addition of the Wakelock package.
App Code That Replicates Issue
import 'package:flutter/material.dart';
import 'package:wakelock/wakelock.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
Wakelock.enable();
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(),
home: const HomeScreen(),
);
}
}
class HomeScreen extends StatelessWidget {
const HomeScreen({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const Scaffold(body: Center(child: Text('Home')));
}
}
...
dependencies:
flutter:
sdk: flutter
wakelock: 0.6.1+2
...
Flutter Doctor Output
[✓] Flutter (Channel unknown, 3.0.1, on macOS 11.6.5 20G527 darwin-x64, locale en-US) [✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0) [✓] Xcode - develop for iOS and macOS (Xcode 13.0) [✓] Chrome - develop for the web [✓] Android Studio (version 2020.3) [✓] Android Studio (version 4.1) [✓] VS Code (version 1.67.2) [✓] Connected device (4 available) [✓] HTTP Host Availability
• No issues found!
It seems to be a timing issue with the app and JS libraries being available , since a call to enable()
after a 500ms+ delay does not cause the error.
void main() {
WidgetsFlutterBinding.ensureInitialized();
Future.delayed(const Duration(seconds: 1), () {
Wakelock.enable();
});
runApp(const MyApp());
}
I'm also seeing this, thanks for the workaround @alexagat .
I'm guessing it's related to https://github.com/creativecreatorormaybenot/wakelock/pull/171
the same here, @alexagat thanks for the workaround!