How can I define methods(functions) for a single model that is not a list with Flutter's Provider
? For example, I have made 4 functions for a Model that is a list:
List<User> _userList = [];
List<User> get userList => _userList;
//method for getiing and setting the list of users
setUserList(List<User> list) {
_userList = list;
notifyListeners();
}
// method for removing a single user
deleteUser(User list) {
_userList.remove(list);
notifyListeners();
}
//adding a new user
addUser(User list) {
_userList.add(list);
notifyListeners();
}
//updating the specific user
updateUser(User user) {
_userList [_userList.indexWhere((element) => element.id == user.id)] = user;
notifyListeners();
}
These all work fine (at least I think they work when I tested them :D) when it's a list of users, but how can I define these methods when it is a single object/item (single User) and not a list? The .add()
, remove()
, are methods that are available when there is a list, but not when there is a single item. What is the best approach for these CRUD model methods? The 'Read' is similar when it is a list:
User get user => _user;
//method for getting the user data
setUser(User user) {
_user = user;
notifyListeners();
}
but how I define the rest of the CRUD model like create(add), update and delete for a single model and not a list?