skip to Main Content

I have a section of my flutter/dart code that I want to include only when building for debugging. I could create a custom define like this:

import 'dart:developer';

void main() {
  #if DEBUG
  log('This message will only be printed in debug mode');
  #endif

  // Other code that runs in both debug and release modes
}

…and use it like this…

flutter build apk --dart-define=DEBUG=true

But, it would be better if I didn’t have to remember to set that flag. Is there a cleaner way to do this?

2

Answers


  1. The KDebugmode Variable might be what you are looking for

    KdebugMode

    Login or Signup to reply.
  2. The flag mentioned by @nlykk12 is the one you need – it works out of the box.
    In your example it would be:

    import 'dart:developer';
    
    void main() {
      if (kDebugMode) {
        log('This message will only be printed in debug mode');
      }
    
      // Other code that runs in both debug and release modes
    }
    

    To build it in debug mode you would only need:

    flutter build apk --debug
    

    To make sure you’re building in a release mode:

    flutter build apk --release
    

    * (the --release and --debug) flags work for other distributions, not only apk.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search