Skip to content
This repository has been archived by the owner on Jul 11, 2024. It is now read-only.

Web - TypeError: Cannot read properties of undefined (reading 'toggle') #176

Closed
alexagat opened this issue May 27, 2022 · 5 comments
Closed

Comments

@alexagat
Copy link

alexagat commented May 27, 2022

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!

@alexagat
Copy link
Author

alexagat commented May 27, 2022

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());
}

@kelvie
Copy link

kelvie commented Jun 10, 2022

I'm also seeing this, thanks for the workaround @alexagat .

@kelvie
Copy link

kelvie commented Jun 10, 2022

I'm guessing it's related to #171

@ova2
Copy link

ova2 commented Feb 6, 2023

the same here, @alexagat thanks for the workaround!

CubeDr pushed a commit to hy-cs-team/jbtimer that referenced this issue Dec 8, 2023
TypeError: Cannot read properties of undefined (reading 'toggle')

creativecreatorormaybenot/wakelock#176
@creativecreatorormaybenot
Copy link
Owner

I will soon discontinue this repository. Please use wakelock_plus: ^1.1.6 instead.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants