Hava Durumu Uygulaması: Önümüzdeki Üç Gün
Coddy'nin Flutter Journey'sinin Veri ve Ağ bölümünün bir parçası. Ders 24 / 27.
Bir şehir için yanıt ayrıca bir tahmin de içerir: nesnenin içinde, her gün için bir öğe bulunan bir JSON listesi.
"forecast": [
{"date": "2026-10-20", "condition": "Cloudy", "high": 22, "low": 15},
...
]Bir nesnenin içindeki liste, tek başına duran bir listeyle aynı şekilde okunur: onu as List<dynamic> olarak al, sonra her öğeyi kendi fromJson yöntemiyle map et. Dosyanın alt kısmındaki Weather.fromJson'a bak: bu işlem zaten burada yapılıyor, dolayısıyla weather.forecast bir List<DayForecast>.
final days = json['forecast'] as List<dynamic>;
forecast: days
.map((day) => DayForecast.fromJson(day as Map<String, dynamic>))
.toList(),Her günün bir DateTime'ı vardır. DateFormat('EEEE') bunu haftanın gününün tam adına, Tuesday'ye dönüştürür. Column'un children'ı içindeki bir for, her gün için bir satır oluşturur.
Görev
OrtaMevcut hava durumunun altında, sonraki üç günü göster:
package:intl/intl.dartpaketini içe aktarSizedBox(height: 32)öğesinden sonra, her gün için birRow:ExpandediçindeDateFormat('EEEE')ile haftanın günü, ardından en yüksek ve en düşük sıcaklık22° / 15°biçiminde- her satıra sol ve sağ taraftan
32piksel dolgu ver
Kendin dene
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,
);
}
}
Bu ders kısa bir quiz içerir. Soruları yanıtlamak ve ilerlemeni kaydetmek için derse başla.
Veri ve Ağ bölümündeki tüm dersler
3Çağrılar Başarısız Olduğunda
try ile Hataları YakalamaHata Fırlatmafinally ile TemizlemeFutureBuilder Hata DurumuYeniden Dene Düğmesi6Proje: Hava Durumu Uygulaması
Hava Durumu Uygulaması: Şu AnHava Durumu Uygulaması: Önümüzdeki Üç GünKendi başına pratik yap: Online Flutter derleyicisi