In later versions, you can use kDebugMode
:
if (kDebugMode)
doSomething();
While asserts can technically be used to manually create an "is debug mode" variable, you should avoid that.
Instead, use the constant kReleaseMode
from package:flutter/foundation.dart
The difference is all about tree shaking.
Tree shaking (aka the compiler removing unused code) depends on variables being constants.
The issue is, with asserts our isInReleaseMode
boolean is not a constant. So when shipping our app, both the dev and release code are included.
On the other hand, kReleaseMode
is a constant. Therefore the compiler is correctly able to remove unused code, and we can safely do:
if (kReleaseMode) {
} else {
// Will be tree-shaked on release builds.
}