loadData method
Implementation
Future<void> loadData() async {
if (!_isLoading) {
_error = null;
_isLoading = true;
notifyListeners();
}
try {
// One-time data loading
if (_loader != null) {
_data = await _loader!();
_error = null;
_isLoading = false;
notifyListeners();
}
// Watching on data changes
else if (_source != null) {
await _subscription?.cancel();
_subscription = _source!().listen(
(result) {
_data = result;
_error = null;
_isLoading = false;
notifyListeners();
},
onError: (error) {
_error = StatefulDataError(
message: 'Unexpected error while watching the data',
originalError: error,
);
_isLoading = false;
notifyListeners();
},
cancelOnError: false,
);
} else {
// We should not hit this branch.
throw Exception('StatefulData must have either "loader" or "watcher"');
}
} catch (e) {
_error = StatefulDataError(message: e.toString(), originalError: e);
_isLoading = false;
notifyListeners();
rethrow;
}
}