Flutter only import library in debug mode

Viewed 612

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?

1 Answers

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.

Related