mobile_adaptive_learning/lib/features/learning/provider/section_provider.dart

47 lines
1.1 KiB
Dart
Raw Normal View History

import 'package:english_learning/core/services/repositories/section_repository.dart';
import 'package:english_learning/features/learning/modules/model/section_model.dart';
import 'package:flutter/foundation.dart';
class SectionProvider extends ChangeNotifier {
final SectionRepository _repository = SectionRepository();
List<Section> _sections = [];
bool _isLoading = false;
dynamic _error;
List<Section> get sections => _sections;
bool get isLoading => _isLoading;
String? get error => _error;
void resetData() {
_sections = [];
_error = null;
_isLoading = true;
notifyListeners();
}
Future<void> fetchSections(String token) async {
try {
// Set loading state
_isLoading = true;
_error = null;
notifyListeners();
// Fetch sections
_sections = await _repository.getSections(token);
// Reset loading state
_isLoading = false;
_error = null;
notifyListeners();
} catch (e) {
// Handle error
_isLoading = false;
_error = e;
notifyListeners();
// Rethrow the error to be handled by the caller
rethrow;
}
}
}