Does Flutter remove debug-mode code when compiling for release?
Asked Answered
M

2

4

I'm wondering whether it is safe to place passwords directly in the Dart code like below. Does Flutter remove the code when compiling it for release? Of course I want to make sure that the code cannot be decompiled such that the username and password can be extracted.

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

if(inDebugMode){
  emailController.text = '[email protected]';
  passwordController.text = 'secret';
}
Moreover answered 9/11, 2018 at 12:3 Comment(0)
P
7

The code you provided won't be tree-shaked. As isInDebugMode isn't a constant.

Instead you can use an assert this way:

assert(() {
  emailController.text = '[email protected]';
  passwordController.text = 'secret';
  return true;
}());
Paiz answered 9/11, 2018 at 12:6 Comment(0)
H
4

Tree-shaking removes that code when inDebugMode is a const value.

"safe" is a strong word for that though even when tree-shaking removes the code.
You could make a mistake that causes tree-shaking to retain the code.
You probably commit the code to a CVS repo.
...

You can use

  • a const value
const bool isProduction = bool.fromEnvironment('dart.vm.product');
if(isProduction) {
  ...
}
  • different lib/main.dart files with flutter run -t lib/debug_main.dart

  • or the assert method as mentioned (see also How to exclude debug code)

Halden answered 9/11, 2018 at 12:4 Comment(1)
Which means no hereBlockbusting

© 2022 - 2024 — McMap. All rights reserved.