88 lines
1.8 KiB
Dart
88 lines
1.8 KiB
Dart
// Local
|
|
import 'package:com_hyperling_buddy_website/secrets.dart';
|
|
|
|
// Flutter / Dart
|
|
import 'package:flutter/material.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'dart:convert';
|
|
|
|
void main() {
|
|
runApp(const MainApp());
|
|
}
|
|
|
|
final String loadText = "Loading...";
|
|
|
|
class MainApp extends StatefulWidget {
|
|
const MainApp({super.key});
|
|
|
|
@override
|
|
State<MainApp> createState() => _MainAppState();
|
|
}
|
|
|
|
class _MainAppState extends State<MainApp> {
|
|
String weather = loadText;
|
|
bool keepLoading = false;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_callOpenWeather();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
weather = loadText;
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
if (weather == loadText) {
|
|
Future.delayed(Duration(seconds: 1), () {
|
|
setState(() {
|
|
keepLoading = true;
|
|
});
|
|
});
|
|
}
|
|
|
|
return MaterialApp(
|
|
home: Scaffold(
|
|
body: Center(
|
|
child: Column(children: [Text('Hello World!'), Text(weather)]),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
// How to guide:
|
|
// https://openweathermap.org/current#zip
|
|
Future<String> _hitOpenWeather(String zipCode, String? countryCode) async {
|
|
countryCode ??= "US";
|
|
|
|
http.Response response = await http.get(
|
|
Uri.parse(
|
|
'https://api.openweathermap.org/data/2.5/forecast'
|
|
'?zip=$zipCode,$countryCode'
|
|
'&units=imperial'
|
|
'&appid=$openWeatherKey',
|
|
),
|
|
);
|
|
|
|
String data = "";
|
|
if (response.statusCode == 200) {
|
|
data = response.body;
|
|
var decodedData = jsonDecode(data);
|
|
print(decodedData);
|
|
data = decodedData.toString();
|
|
} else {
|
|
print(response.statusCode);
|
|
}
|
|
|
|
return data;
|
|
}
|
|
|
|
_callOpenWeather() async {
|
|
weather = await _hitOpenWeather("47630", "US");
|
|
}
|
|
}
|