Flutter only import library in debug mode

498

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.

Share:
498
Adam Griffiths
Author by

Adam Griffiths

I work as part of the Alexa Shopping team at Amazon, making shopping easier and more interactive for customers around the globe.

Updated on December 27, 2022

Comments

  • Adam Griffiths
    Adam Griffiths over 1 year

    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?

    • Abdur Rohman
      Abdur Rohman about 3 years
      dev dependencies dart can be declared in pubspec.yaml, you can check on this documentation. I am sorry for my bad.
    • Adam Griffiths
      Adam Griffiths about 3 years
      @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
    • Rehmat Singh Gill
      Rehmat Singh Gill about 3 years
      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?
  • K.Amanov
    K.Amanov almost 3 years
    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."
  • Doc
    Doc almost 3 years
    @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.
  • K.Amanov
    K.Amanov almost 3 years
    I moved 3 ligraries to dev depedencies, but in release mode app size didn't changed as supposed. Thank you.