Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to differentiate between debug and release mode in flutter in code? [duplicate]

Tags:

flutter

dart

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.

like image 280
RoyalGriffin Avatar asked Apr 10 '19 07:04

RoyalGriffin


People also ask

How do I know if flutter is in debug mode?

The easiest way is to use assert as it only runs in debug mode. Here's an example from Flutter's Navigator source code: assert(() { if (navigator == null && ! nullOk) { throw new FlutterError( 'Navigator operation requested with a context that does not include a Navigator.

What is the difference between debug mode and Release mode?

Debug Mode: In debug mode the application will be slow. Release Mode: In release mode the application will be faster. Debug Mode: In the debug mode code, which is under the debug, symbols will be executed. Release Mode: In release mode code, which is under the debug, symbols will not be executed.

What is the difference between debug mode and profile mode in flutter?

Use debug mode during development, when you want to use hot reload. Use profile mode when you want to analyze performance. Use release mode when you are ready to release your app.


2 Answers

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.

like image 186
Rémi Rousselet Avatar answered Oct 23 '22 08:10

Rémi Rousselet


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');
}
like image 39
Kalpesh Kundanani Avatar answered Oct 23 '22 07:10

Kalpesh Kundanani