Search⌘ K
AI Features

Solution: Weather Server Outage

Understand how to handle network failures and errors in Dart asynchronous code through practical use of try, catch, and finally blocks. Explore how to safely manage exceptions during data fetching to maintain app stability and improve error reporting.

We'll cover the following...
Dart
Future<double> fetchTemperature() async {
await Future.delayed(Duration(seconds: 1));
throw Exception('Server timeout');
}
void main() async {
try {
final temp = await fetchTemperature();
print('Current temperature: $temp');
} catch (error) {
print('Error loading weather: $error');
} finally {
print('Closing weather connection.');
}
}

Solution explanation

In the main.dart file:

  • Lines 1—4: We ...