8

I want to have some functionality only in release mode, and not in debug. It's longer to get past it and just commenting it during development is not a good idea. As there is always a probability of forgetting about it when making release builds.

RoyalGriffin
  • 1,987
  • 1
  • 12
  • 25

2 Answers2

25

By importing flutter/foundation.dart, a top level constant is available for this check:

kReleaseMode

This is better than asserts, because it works with tree shaking.

Rémi Rousselet
  • 256,336
  • 79
  • 519
  • 432
4

This worked well for me. Declare a function like following;

bool get isInDebugMode {
  bool inDebugMode = false;
  assert(inDebugMode = true);
  return inDebugMode;
}

Now you can use it like:

if(isInDebugMode) {
    print('Debug');
} else {
    print('Release');
}

Source of information

======================================================================== You can also use solution given by @Rémi Rousselet:

First import the package:

import 'package:flutter/foundation.dart';

and use kReleaseMode like this:

if(kReleaseMode) { // is in Release Mode ?
    print('Release');
} else {
    print('Debug');
}
Kalpesh Kundanani
  • 5,413
  • 4
  • 22
  • 32