3

I am using Android 11 Wireless Debugging to develop my app. Whenever the device automatically locks itself, it takes a while to re-establish the connection for hot reloading.

To overcome this I am using wakelock, which I only need to use if my app is in debug mode, not in release mode.

In lib/main.dart I have the following code:

import 'package:flutter/foundation.dart' as Foundation;
import 'package:wakelock/wakelock.dart';

...

void main() {
  if (Foundation.kDebugMode) {
    Wakelock.enable();
  }
  runApp(App());
}

As you can see the wakelock package is only used if the app is running in debug mode.

Is there a way to only import wakelock if the app is running in debug mode?

3
  • dev dependencies dart can be declared in pubspec.yaml, you can check on this documentation. I am sorry for my bad. Commented Feb 7, 2021 at 13:01
  • @AbdurRohman I have updated my question with more context. Sadly dev dependencies wouldn't work in this case since I am importing the package into lib/main.dart Commented Feb 7, 2021 at 13:32
  • So you need to use wake lock only to prevent the screen from turning off? If yes, then why not change sleep settings of your device? Commented Feb 7, 2021 at 16:18

1 Answer 1

2

Tested it as

pubspec.yaml

dev_dependencies:
  wakelock: ^0.2.1+1

Usage

import 'package:flutter/foundation.dart';
import 'package:wakelock/wakelock.dart';
import 'package:flutter/material.dart';

main() async {
  WidgetsFlutterBinding.ensureInitialized();

  if (kDebugMode) {
    print('activating wakelock in debug');
    Wakelock.enable();
  }

  runApp(App());
}

Sidenote:

If all you need is the device to stop locking itself after some time then try increasing the sleep delay under the Display setting on the device itself, or use a setting in developer options called Stay awake while charging which allows the device to stay on forever while charging.

Sign up to request clarification or add additional context in comments.

9 Comments

But the documentation says: "If the dependency is imported from something in your lib or bin directories, it needs to be a regular dependency. If it’s only imported from test, example, etc. it can and should be a dev dependency."
@ZEHINZ OP wanted a solution where the device would not go to sleep while developing the code. There was no intention of actually keep the screen awake in production mode. Thus, dev_dependency works fine.
I moved 3 ligraries to dev depedencies, but in release mode app size didn't changed as supposed. Thank you.
This solution is wrong, because it will still ship the dependency, even in release mode.
@MartinBraun What makes you believe that? dev_dependencies do not ship in release mode. That's the whole point.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.