By Liber Da Silva
Removing unused code from the final application
const color = Color(0xFFC4C4C4);
const apiKey = String.fromEnvironment('API_KEY');
const int value1 = 10;
const int value2 = 20;
const int sumValues = value1 + value2; // Calculated at compile-time
class MyWidget extends StatelessWidget {
const MyWidget({Key? key, required this.title}) : super(key: key);
final String title;
@override
Widget build(BuildContext context) {
return Text(title);
}
}
const bool isDebug = bool.fromEnvironment('DEBUG');
void main() {
if (isDebug) {
// This code will be removed in production
setupDebugTools();
}
}
flutter run --dart-define=API_KEY=my_api_key
const apiKey = String.fromEnvironment('API_KEY');
Create a .env (or .json) file:
API_KEY=my_api_key
DEBUG_MODE=true
Run with:
flutter run --dart-define-from-file=.env
Access in code:
const apiKey = String.fromEnvironment('API_KEY');
const debugMode = bool.fromEnvironment('DEBUG_MODE');
Let's code!