Initial Commit
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:wetter/models/daily_weather_model.dart';
|
||||
import 'package:wetter/services/brightsky_api_service.dart';
|
||||
|
||||
void main() => runApp(const MyApp());
|
||||
|
||||
class MyApp extends StatefulWidget {
|
||||
const MyApp({super.key});
|
||||
|
||||
@override
|
||||
State<MyApp> createState() => _MyAppState();
|
||||
}
|
||||
|
||||
class _MyAppState extends State<MyApp> {
|
||||
late Future<DailyWeatherModel> futureAlbum;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
futureAlbum = BrightSkyAPI.fetchForecast();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
title: 'Fetch Data Example',
|
||||
theme: ThemeData(
|
||||
primarySwatch: Colors.blue,
|
||||
),
|
||||
home: Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Fetch Data Example'),
|
||||
),
|
||||
body: Center(
|
||||
child: FutureBuilder<DailyWeatherModel>(
|
||||
future: futureAlbum,
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.hasData) {
|
||||
return Text(snapshot.data!.dailyAverageTemp());
|
||||
} else if (snapshot.hasError) {
|
||||
return Text('${snapshot.error}');
|
||||
}
|
||||
|
||||
// By default, show a loading spinner.
|
||||
return const CircularProgressIndicator();
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import 'package:wetter/models/hourly_weather_model.dart';
|
||||
|
||||
class DailyWeatherModel {
|
||||
late List<HourlyWeatherModel> hourlyForecasts;
|
||||
|
||||
DailyWeatherModel.fromJson(Map<String, dynamic> json) {
|
||||
List dataPoints = json['weather'];
|
||||
hourlyForecasts = [];
|
||||
for (var dataPoint in dataPoints) {
|
||||
hourlyForecasts.add(HourlyWeatherModel.fromJson(dataPoint));
|
||||
}
|
||||
}
|
||||
|
||||
String dailyAverageTemp() {
|
||||
double tempSum = 0;
|
||||
for (var forecast in hourlyForecasts) {
|
||||
tempSum = tempSum + forecast.temperature;
|
||||
}
|
||||
return (tempSum / hourlyForecasts.length).toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
class HourlyWeatherModel {
|
||||
late DateTime timestamp;
|
||||
String? sourceId;
|
||||
double? precipitation;
|
||||
int? pressure;
|
||||
int? sunshine;
|
||||
late double temperature;
|
||||
int? windDirection;
|
||||
double? windSpeed;
|
||||
int? cloudCover;
|
||||
double? dewPoint;
|
||||
int? relativeHumidity;
|
||||
int? visibility;
|
||||
int? windGustDirection;
|
||||
double? windGustSpeed;
|
||||
String? condition;
|
||||
String? icon;
|
||||
|
||||
HourlyWeatherModel();
|
||||
|
||||
HourlyWeatherModel.fromJson(Map<String, dynamic> json) {
|
||||
sourceId = json['source_id'].toString();
|
||||
precipitation = json['precipitation'];
|
||||
pressure = json['pressure'];
|
||||
sunshine = json['sundshine'];
|
||||
temperature = json['temperature'];
|
||||
windDirection = json['wind_direction'];
|
||||
windSpeed = json['wind_speed'];
|
||||
cloudCover = json['cloud_cover'];
|
||||
dewPoint = json['dew_point'];
|
||||
relativeHumidity = json['relative_humidity'];
|
||||
visibility = json['visibility'];
|
||||
windGustDirection = json['wind_gust_direction'];
|
||||
windGustSpeed = json['wind_gust_speed'];
|
||||
condition = json['condition'];
|
||||
icon = json['icon'];
|
||||
timestamp = DateTime.parse(json['timestamp']);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import 'dart:convert';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:wetter/models/daily_weather_model.dart';
|
||||
|
||||
class BrightSkyAPI {
|
||||
static Future<DailyWeatherModel> fetchForecast() async {
|
||||
final response = await http.get(
|
||||
Uri.parse(
|
||||
'https://api.brightsky.dev/weather?lat=53.43&lon=20&date=2022-08-30&tz=Europe/Berlin'),
|
||||
headers: {"Accept": "application/json"});
|
||||
if (response.statusCode == 200) {
|
||||
return DailyWeatherModel.fromJson(jsonDecode(response.body));
|
||||
} else {
|
||||
throw Exception('Failed to load weather data');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user