Created
March 19, 2019 12:14
-
-
Save apaatsio/0aa9389b7db61f346828c35ca4f0412b to your computer and use it in GitHub Desktop.
Helper utility to detect build mode (debug, profile, and release) in a Flutter app.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class BuildMode { | |
static final BuildModeType buildMode = () { | |
if (const bool.fromEnvironment('dart.vm.product')) { | |
return BuildModeType.release; | |
} | |
var result = BuildModeType.profile; | |
assert(() { | |
result = BuildModeType.debug; | |
return true; | |
}()); | |
return result; | |
}(); | |
static final bool isDebug = buildMode == BuildModeType.debug; | |
static final bool isNotDebug = !isDebug; | |
static final bool isProfile = buildMode == BuildModeType.profile; | |
static final bool isNotProfile = !isProfile; | |
static final bool isRelease = buildMode == BuildModeType.release; | |
static final bool isNotRelease = !isRelease; | |
} | |
enum BuildModeType { | |
debug, | |
profile, | |
release, | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import './build_mode.dart'; | |
print('App is running in ${BuildMode.buildMode}'); | |
if(BuildMode.isRelease) { | |
initializeAnalytics(); | |
} | |
if(BuildMode.isDebug) { | |
print('Some debugging info.'); | |
// Note: to print debug info, check out also debugPrint() as an alternative | |
// See https://flutter.dev/docs/testing/debugging#print-and-debugprint-with-flutter-logs | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment