Dart checks if it is building

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

Example:

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

I would like the above code not to be passed to JS during production.

+3
source share
1 answer

Assessors are executed only in verification mode and will not be enabled in the pub builddefault production mode:

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

Example DartPad does not print it because it builds in run mode.

You can also pass the "environment" (without mixing with OS environment variables) before pub buildand 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 fooobar.com/questions/1532569 / ...

+3

Source: https://habr.com/ru/post/1532572/


All Articles