Why should you use kReleaseMode instead of assert?

Technology CommunityCategory: FlutterWhy should you use kReleaseMode instead of assert?
VietMX Staff asked 3 years ago

The difference is all about tree shakingTree shaking (aka the compiler removing unused code) depends on variables being constants.

The issue is, with assert 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:

import 'package:flutter/foundation.dart' as Foundation;

if (Foundation.kReleaseMode) {
	// Code
} else {
	// Will be tree-shaked on release builds.
 }