Hava Durumu Uygulaması: Şu An
Coddy'nin Flutter Journey'sinin Veri ve Ağ bölümünün bir parçası. Ders 23 / 27.
Bu bölümden koca bir uygulama yapma zamanı: bir hava durumu uygulaması. Önümüzdeki beş derste bir şehrin şu anki hava durumunu, sonraki üç günün hava durumunu, her gün için bir simgeyi, “Tekrar dene” seçeneği olan bir hata ekranını ve şehir seçiciyi göstereceksin.
Alıştırma API'si üç şehri tanıyor. /weather/lisbon şu yanıtı verir:
{
"city": "Lisbon",
"temperature": 21,
"condition": "Sunny",
"forecast": [ ... ]
}Ekranın en üstünden ve bildiğin adımlardan başla: veriler için bir sınıf, verileri alan ve çözen bir işlev, initState içinde bir kez oluşturulan bir future ve üç durumunu içeren bir FutureBuilder. Dosyanın altındaki Weather ve DayForecast sınıfları, fromJson yöntemleriyle birlikte senin için yazıldı. Bu derste mevcut hava durumunu göstereceksin; tahmin bir sonraki derste gelecek.
Görev
ZorLizbon'un hava durumunu göster:
loadWeather,/weather/lisbonverisini alır veWeather.fromJson(jsonDecode(body))döndürür- geleceği
initStateiçinde bir kez oluştur - bir
FutureBuilder<Weather>, yükleme sırasında bir dönen simge gösterir; yükleme başarısız olursaCould not load:metnini ve hatayı gösterir - veriler geldiğinde; şehri (boyut 32), derece işaretli sıcaklığı
21°(boyut 72, kalın) ve hava durumunu (boyut 24) içeren birColumngöster
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> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Weather')),
body: const Center(
child: Text('The weather goes here'),
),
);
}
}
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 DurumuTekrar 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