Fixed bug - Loading Indicator now functions correctly

This commit is contained in:
2025-01-27 21:08:41 +01:00
parent 13bd344807
commit 36dec2d0de
5 changed files with 106 additions and 29 deletions

View File

@@ -21,6 +21,20 @@ class Api {
return stations;
}
/// Fetches all stations whose label contain [label]
static Future<List<FloodStation>> fetchFilteredStations(String label) async {
List<FloodStation> stations = [];
final response =
await http.get(Uri.parse('$_rootUrl/id/stations?search=$label'));
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;
}
/// [limit] limits the number of entries that are requested from the API and [offset] returns the
/// list starting from the specified number

View File

@@ -1,14 +1,22 @@
import 'package:flutter/material.dart';
import 'package:async/async.dart';
import '../model/flood_station.dart';
import 'api.dart';
class FloodStationProvider extends ChangeNotifier {
FloodStation? selectedStation;
bool filtered = false;
List<FloodStation> _allStations = [];
List<FloodStation> _filteredStations = [];
List<FloodStation> get allStations => _allStations;
List<FloodStation> get filteredStations => _filteredStations;
CancelableOperation? _filteredStationsFuture;
CancelableOperation? get filteredStationsFuture => _filteredStationsFuture;
Future loadAllStationsInBatches({silent = false}) {
int offset = 0;
@@ -37,4 +45,26 @@ class FloodStationProvider extends ChangeNotifier {
},
);
}
Future loadFilteredStations(String filter, {silent = false}) {
if (_filteredStationsFuture != null) {
_filteredStationsFuture!.cancel();
}
final future = Api.fetchFilteredStations(filter);
_filteredStationsFuture = CancelableOperation.fromFuture(future).then(
(value) {
_filteredStations = value;
if (!silent) {
notifyListeners();
}
},
);
return future;
}
void cancelFilterLoading() {
if (_filteredStationsFuture != null) {
_filteredStationsFuture!.cancel();
}
}
}