How to disable all Logs [debugPrint()] in release build in flutter?
8,513
Solution 1
I combined the accepted answer with the idea from here and used it main.dart to silence all debugPrint everywhere.
const bool isProduction = bool.fromEnvironment('dart.vm.product');
void main() {
if (isProduction) {
// analyser does not like empty function body
// debugPrint = (String message, {int wrapWidth}) {};
// so i changed it to this:
debugPrint = (String? message, {int? wrapWidth}) => null;
}
runApp(
MyApp()
);
}
Solution 2
You can assign a dummy function to the global debugPrint
variable:
import 'package:flutter/material.dart';
main() {
debugPrint = (String message, {int wrapWidth}) {};
}
Author by
Ajay Kumar
I'm a Technical Lead Flutter in Pune, India. I am passionate about building excellent software that improves the lives of those around me.
Updated on December 04, 2022Comments
-
Ajay Kumar over 1 year
I have installed a release build apk in the android device but if I connect that device to Android studio then I am able to see all Logs/debugPrint statements.
Is there any way to disable the all the logs ?
-
shadowsheep about 6 yearsAnd how could I check the build variant so that I can do it only when I compile for production? I don't like this solution: stackoverflow.com/questions/47438564/… and I don't wanna use package like simple_preprocessor.
-
Günter Zöchbauer about 6 yearsI think the linked SO question us the way to go. You can also create a build script that modifies a source file before it calls
flutter build ...
-
shadowsheep about 6 yearsDunno, but thanks. I’ll look forward this custom build script way. If you already have some links they’ll be appreciated ;)
-
Günter Zöchbauer about 6 yearsYou can use a simple shell script or a bash script. I have a project where I copy a different Dart file that contains some config values that are imported somewhere into place before building. This way I don't need to modify a files content
-
shadowsheep about 6 yearsI’ll do some further research about this topic. Maybe I’ll post a specific question. For now upvoted your answer! ;)
-
K Pradeep Kumar Reddy almost 4 yearsThe link you have given is not working. Getting page not found error.
-
Sebastian almost 4 yearsi fixed the link. no idea what went wrong before :(
-
mortred95 over 3 years@Sebastian I think you always check the bool kDebugMode/kReleaseMode which is a constant provided by Flutter's foundation.
-
bounxye almost 3 yearswill compiler remove all calls for function which does nothing?
-
Sebastian almost 3 yearsI know nothing about that, but it is mentioned here that it does: stackoverflow.com/questions/53256139/…