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}) {};
}
Share:
8,513
Ajay Kumar
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, 2022

Comments

  • Ajay Kumar
    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
    shadowsheep about 6 years
    And 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
    Günter Zöchbauer about 6 years
    I 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
    shadowsheep about 6 years
    Dunno, 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
    Günter Zöchbauer about 6 years
    You 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
    shadowsheep about 6 years
    I’ll do some further research about this topic. Maybe I’ll post a specific question. For now upvoted your answer! ;)
  • K Pradeep Kumar Reddy
    K Pradeep Kumar Reddy almost 4 years
    The link you have given is not working. Getting page not found error.
  • Sebastian
    Sebastian almost 4 years
    i fixed the link. no idea what went wrong before :(
  • mortred95
    mortred95 over 3 years
    @Sebastian I think you always check the bool kDebugMode/kReleaseMode which is a constant provided by Flutter's foundation.
  • bounxye
    bounxye almost 3 years
    will compiler remove all calls for function which does nothing?
  • Sebastian
    Sebastian almost 3 years
    I know nothing about that, but it is mentioned here that it does: stackoverflow.com/questions/53256139/…