Application météo : réessayer
Fait partie de la section Données et réseau du Journey Flutter de Coddy. Leçon 26 sur 27.
Les applications météo sont ouvertes dans les trains, les tunnels et en montagne. Sans signal, l’application doit l’indiquer clairement et proposer un moyen de revenir en arrière, au lieu d’afficher un indicateur de chargement sans fin ou une ligne de texte d’erreur.
Donne à l’erreur son propre petit écran : une icône, une phrase facile à comprendre, les détails en dessous et un bouton Réessayer qui crée un nouvel avenir dans setState. Place-le dans sa propre méthode, buildError, à côté de buildWeather, pour que build reste court.
if (snapshot.connectionState == ConnectionState.waiting) {
return const CircularProgressIndicator();
}
if (snapshot.hasError) {
return buildError(snapshot.error!);
}
return buildWeather(snapshot.data!);Vérifier ConnectionState.waiting en premier affiche l’indicateur de chargement pendant chaque chargement, y compris lors de la deuxième tentative. Une fois l’avenir terminé, il y a soit une erreur, soit des données ; la dernière ligne peut donc utiliser snapshot.data!.
Pour voir le résultat dans l’aperçu, écris offline = true; comme première ligne de main, puis appuie sur Run. Supprime cette ligne avant d’appuyer sur Check.
Défi
MoyenAjoute un écran d’erreur à l’application :
- une méthode
buildError(Object error):Icons.cloud_off(taille 64),Could not load the weather(taille 20), le texte de l’erreur et un bouton Try again - une méthode
tryAgainqui définit un nouveauloadWeather()danssetState - dans le builder, vérifie d’abord
ConnectionState.waiting, puis l’erreur, puis affiche la météo
Essayez vous-même
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'api.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
debugShowCheckedModeBanner: false,
home: WeatherScreen(),
);
}
}
class WeatherScreen extends StatefulWidget {
const WeatherScreen({super.key});
@override
State<WeatherScreen> createState() => _WeatherScreenState();
}
class _WeatherScreenState extends State<WeatherScreen> {
late Future<Weather> _weather;
@override
void initState() {
super.initState();
_weather = loadWeather();
}
Future<Weather> loadWeather() async {
final body = await fetch('/weather/lisbon');
return Weather.fromJson(jsonDecode(body));
}
IconData iconFor(String condition) {
return switch (condition) {
'Sunny' => Icons.wb_sunny,
'Cloudy' => Icons.cloud,
'Rainy' => Icons.umbrella,
'Snowy' => Icons.ac_unit,
_ => Icons.help_outline,
};
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Weather')),
body: Center(
child: FutureBuilder<Weather>(
future: _weather,
builder: (context, snapshot) {
if (snapshot.hasError) {
return Text('Could not load: ${snapshot.error}');
}
if (snapshot.hasData) {
return buildWeather(snapshot.data!);
}
return const CircularProgressIndicator();
},
),
),
);
}
Widget buildWeather(Weather weather) {
return Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(weather.city, style: const TextStyle(fontSize: 32)),
Icon(iconFor(weather.condition), size: 96, color: Colors.orange),
Text(
'${weather.temperature}°',
style: const TextStyle(fontSize: 72, fontWeight: FontWeight.bold),
),
Text(weather.condition, style: const TextStyle(fontSize: 24)),
const SizedBox(height: 32),
for (final day in weather.forecast)
Padding(
padding: const EdgeInsets.symmetric(horizontal: 32, vertical: 8),
child: Row(
children: [
Icon(iconFor(day.condition)),
const SizedBox(width: 16),
Expanded(
child: Text(
DateFormat('EEEE').format(day.date),
style: const TextStyle(fontSize: 18),
),
),
Text('${day.high}° / ${day.low}°', style: const TextStyle(fontSize: 18)),
],
),
),
],
);
}
}
class Weather {
Weather({
required this.city,
required this.temperature,
required this.condition,
required this.forecast,
});
final String city;
final int temperature;
final String condition;
final List<DayForecast> forecast;
factory Weather.fromJson(Map<String, dynamic> json) {
final days = json['forecast'] as List<dynamic>;
return Weather(
city: json['city'] as String,
temperature: json['temperature'] as int,
condition: json['condition'] as String,
forecast: days
.map((day) => DayForecast.fromJson(day as Map<String, dynamic>))
.toList(),
);
}
}
class DayForecast {
DayForecast({
required this.date,
required this.condition,
required this.high,
required this.low,
});
final DateTime date;
final String condition;
final int high;
final int low;
factory DayForecast.fromJson(Map<String, dynamic> json) {
return DayForecast(
date: DateTime.parse(json['date'] as String),
condition: json['condition'] as String,
high: json['high'] as int,
low: json['low'] as int,
);
}
}
Cette leçon comprend un petit quiz. Commencez la leçon pour y répondre et suivre votre progression.
Toutes les leçons de Données et réseau
3Quand les appels échouent
Intercepter les erreurs avec tryDéclencher une erreurNettoyer avec finallyL’état d’erreur de FutureBuilderUn bouton pour réessayer6Projet : application météo
Application météo : maintenantApplication météo : les trois prochains joursEntraînez-vous par vous-même : Compilateur Flutter en ligne