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.
            Asked
            
        
        
            Active
            
        
            Viewed 7,499 times
        
    8
            
            
        - 
                    1see https://flutter.dev/docs/testing/debugging#debug-mode-assertions – pskink Apr 10 '19 at 07:15
 
2 Answers
25
            By importing flutter/foundation.dart, a top level constant is available for this check:
This is better than asserts, because it works with tree shaking.
        Rémi Rousselet
        
- 256,336
 - 79
 - 519
 - 432
 
- 
                    3
 - 
                    8The compiler will remove unused code. So if you're doing `if (kReleaseMode) {} else {}` then the compiler knows that the `else` will never be executed, and therefore remove it. – Rémi Rousselet Apr 10 '19 at 12:33
 
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');
}
======================================================================== 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