46 lines
1.1 KiB
Dart
46 lines
1.1 KiB
Dart
// File for helper functions.
|
|
|
|
import '/var/config.dart';
|
|
import '/var/secrets.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'dart:convert';
|
|
|
|
// Generic method to hit a GET request and return the response.
|
|
Future<String> hitAPI(String url) async {
|
|
if (debug) debugPrint(url);
|
|
|
|
http.Response response = await http.get(Uri.parse(url));
|
|
|
|
String data = "";
|
|
if (response.statusCode == 200) {
|
|
data = response.body;
|
|
var decodedData = jsonDecode(data);
|
|
if (debugVerbose) debugPrint(decodedData.toString());
|
|
data = decodedData.toString();
|
|
} else {
|
|
if (debug) debugPrint(response.statusCode.toString());
|
|
}
|
|
|
|
return data;
|
|
}
|
|
|
|
// How to guide: https://openweathermap.org/current#zip
|
|
Future<String> hitOpenWeatherZip(String zipCode, String? countryCode) async {
|
|
countryCode ??= "US";
|
|
|
|
String url =
|
|
'$openWeatherAPI'
|
|
'?zip=$zipCode,$countryCode'
|
|
'&units=imperial'
|
|
'&appid=$openWeatherKey';
|
|
|
|
String data = await hitAPI(url);
|
|
return data;
|
|
}
|
|
|
|
// Convert the Future value into a usable value.
|
|
loadWeather() async {
|
|
return await hitOpenWeatherZip("47630", "US");
|
|
}
|