Chepuhagram/lib/logic/contact_provider.dart

36 lines
933 B
Dart

import 'package:flutter/material.dart';
import '/data/models/contact_model.dart';
import '/data/repositories/contact_repository.dart';
class ContactProvider extends ChangeNotifier {
final ContactRepository _repository = ContactRepository();
List<Contact> _contacts = [];
bool _isLoading = false;
String? _error;
int? _currentUserId;
List<Contact> get contacts => _contacts;
bool get isLoading => _isLoading;
String? get error => _error;
void setCurrentUserId(int? id) {
_currentUserId = id;
notifyListeners();
}
Future<void> loadContacts() async {
_isLoading = true;
_error = null;
notifyListeners();
try {
final allContacts = await _repository.fetchContacts();
_contacts = allContacts.where((contact) => contact.id != _currentUserId).toList();
} catch (e) {
_error = e.toString();
} finally {
_isLoading = false;
notifyListeners();
}
}
}