Flutter的四種運行模式:Debug、Release、Profile和test ,在實際開發中,我們往往需要根據當前運行模式的不同,選擇不同的操作,比如在Debug模式啟用Log、在生產模式關閉Log。
如果你是一名Android開發者,肯定對於這個不陌生,在Android中,有一個根據gradle配置自動生成的BuildConfig類來判斷當前的運行模式。同樣的,在Flutter里面也是有方法來判斷的,我們需要用到 dart.vm.product 環境標識位,具體使用方法為:
const bool inProduction = const bool.fromEnvironment("dart.vm.product");
當App運行在Release環境時,inProduction為true;當App運行在Debug和Profile環境時,inProduction為false。
Release:const bool.fromEnvironment("dart.vm.product") = true;
Debug:assert(() { ...; return true; });斷言語句會被執行;
Profile:上面的兩種情況均不會發生。