Search⌘ K
AI Features

Solution: Smart Thermostat Configuration

Explore how to implement a smart thermostat configuration in Dart by defining functions with named parameters. Understand enforcing required arguments, setting default values, and safely handling nullable options to write robust and maintainable code.

We'll cover the following...
Dart
void configureThermostat({required double targetTemp, String fanMode = 'auto', String? schedule}) {
print('Target: $targetTemp');
print('Fan: $fanMode');
if (schedule != null) {
print('Schedule: $schedule');
}
}
void main() {
configureThermostat(targetTemp: 72.5);
configureThermostat(targetTemp: 68.0, fanMode: 'on');
}

Solution explanation

In the main.dart file:

  • Line 1: We define ...