Implemented way to fetch stations in batches

This commit is contained in:
2025-01-27 19:14:58 +01:00
parent 5df67920ea
commit 9e37ebbc22
2 changed files with 38 additions and 3 deletions

View File

@@ -8,7 +8,7 @@ class Api {
static const String _rootUrl =
'https://environment.data.gov.uk/flood-monitoring';
static Future<List<FloodStation>> fetchStations() async {
static Future<List<FloodStation>> fetchAllStations() async {
List<FloodStation> stations = [];
final response = await http.get(Uri.parse('$_rootUrl/id/stations'));
if (response.statusCode == 200) {
@@ -20,6 +20,23 @@ class Api {
return stations;
}
/// [limit] limits the number of entries that are requested from the API and [offset] returns the
/// list starting from the specified number
static Future<List<FloodStation>> fetchStationsByRange(
int limit, int offset) async {
List<FloodStation> stations = [];
final response = await http
.get(Uri.parse('$_rootUrl/id/stations?_limit=$limit&_offset=$offset'));
if (response.statusCode == 200) {
final Map<String, dynamic> jsonStr = jsonDecode(response.body);
for (final str in jsonStr['items']) {
stations.add(FloodStation.fromMap(str));
}
}
return stations;
}
static Future<List<Reading>> fetchReadingsFromStation(
String stationId) async {
List<Reading> readings = [];