dart check if is building

Issue

I would like to skip some specific code on pub build.

example:

Log.print(‘something ${StackTrace.current}’);

I would like that the code above was not transpilled to JS in production.

Solution

Asserts are only executed in checked mode and won’t be included by pub build in production mode by default:

assert(() {
  Log.print('something ${StackTrace.current}');
  return true;
})

DartPad example doesn’t print it because it builds in production mode.

You can also pass “environment” (not mix up with OS environment variables) to pub build and read it in code

transformers: # or dev_transformers
- $dart2js:
  environment: { PROD: "true" }
const prod = String.fromEnvironment('PROD')
print('PROD: $prod');
// prints 'PROD: null' in Dartium
// prints 'PROD: true' in Chrome

See also https://stackoverflow.com/a/22524258/217408

Answered By – Günter Zöchbauer

Answer Checked By – Mildred Charles (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.