Aplicativo de Previsão do Tempo: Próximos Três Dias
Parte da seção Dados e rede do Journey de Flutter da Coddy. Lição 24 de 27.
A resposta para uma cidade também contém uma previsão: uma lista JSON dentro do objeto, com um item por dia.
"forecast": [
{"date": "2026-10-20", "condition": "Cloudy", "high": 22, "low": 15},
...
]Uma lista dentro de um objeto é lida da mesma forma que uma lista isolada: extraia-a como as List<dynamic> e, em seguida, aplique map a cada item usando seu próprio fromJson. Veja Weather.fromJson na parte inferior do arquivo: ele já faz isso, então weather.forecast é uma List<DayForecast>.
final days = json['forecast'] as List<dynamic>;
forecast: days
.map((day) => DayForecast.fromJson(day as Map<String, dynamic>))
.toList(),Cada dia tem um DateTime. DateFormat('EEEE') o transforma no nome completo do dia da semana, Tuesday. Um for dentro dos filhos de Column cria uma linha para cada dia.
Desafio
MédioAbaixo do clima atual, mostre os próximos três dias:
- importe
package:intl/intl.dart - após um
SizedBox(height: 32), umaRowpor dia: o dia da semana comDateFormat('EEEE'), em umExpanded, seguido das temperaturas máxima e mínima como22° / 15° - adicione 32 pixels de espaçamento interno à esquerda e à direita de cada linha
Experimente você mesmo
import 'dart:convert';
import 'package:flutter/material.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));
}
@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)),
Text(
'${weather.temperature}°',
style: const TextStyle(fontSize: 72, fontWeight: FontWeight.bold),
),
Text(weather.condition, style: const TextStyle(fontSize: 24)),
],
);
}
}
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,
);
}
}
Esta lição inclui um quiz rápido. Comece a lição para respondê-lo e acompanhar seu progresso.
Todas as lições de Dados e rede
3Quando as chamadas falham
Capturando erros com tryLançando um erroFazendo a limpeza com finallyO estado de erro do FutureBuilderUm botão para tentar novamente6Projeto: Aplicativo de Previsão do Tempo
Aplicativo de Previsão do Tempo: AgoraAplicativo de Previsão do Tempo: Próximos Três DiasPratique por conta própria: Compilador de Flutter online