Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
865 views
in Technique[技术] by (71.8m points)

debugging - How to check flutter application is running in debug?

I have a short question. I'm looking for a way to execute code in Flutter when the app is in Debug mode. Is that possible in Flutter? I can't seem to find it anywhere in the documentation.

Something like this

If(app.inDebugMode) {
   print("Print only in debug mode");
}

How to check if the flutter application is running in debug or release mode?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

While asserts technically works, you should not use them.

Instead, use the constant kReleaseMode from package:flutter/foundation.dart


The difference is all about tree shaking

Tree shaking (aka the compiler removing unused code) depends on variables being constants.

The issue is, with asserts our isInReleaseMode boolean is not a constant. So when shipping our app, both the dev and release code are included.

On the other hand, kReleaseMode is a constant. Therefore the compiler is correctly able to remove unused code, and we can safely do:

if (kReleaseMode) {

} else {
  // Will be tree-shaked on release builds.
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...