I want to instantaite the properties of a class while trying to create a object of it in flutter
DataBaseProvider File
class DatabaseProvider extends ChangeNotifier {
int _selectedIndex = 0;
Box<Contacts> _contactsBox = Hive.box<Contacts>('contacts');
Contacts _selectedContact = Contacts();
Box<Contacts> get contactsBox => _contactsBox;
Contacts get selectedContact => _selectedContact;
///* Updating the current selected index for that contact to pass to read from hive
void updateSelectedIndex(int index) {
_selectedIndex = index;
updateSelectedContact();
notifyListeners();
}
///* Updating the current selected contact from hive
void updateSelectedContact() {
_selectedContact = readFromHive();
notifyListeners();
}
///* reading the current selected contact from hive
Contacts readFromHive() {
Contacts getContact = _contactsBox.getAt(_selectedIndex);
return getContact;
}
void deleteFromHive(){
_contactsBox.deleteAt(_selectedIndex);
}
}
Model File
@HiveType(typeId: 0)
class Contacts {
@HiveField(0)
String name;
@HiveField(1)
String imageURL;
@HiveField(2)
String? number;
Contacts({
required this.name,
required this.imageURL,
this.number,
});
}
Now whenever i try creating a object of Contacts Class it aks for the reuqired properties values. For Ex: Contacts ct = Contacts( name: name, imageURL:imageURL)
So in the DatabaseProvider file how shall i initialize these above properties(name and imageURL as it is required) so that I am able to create a object of the class Contacts
You can just provide some default parameters inside the model file -
@HiveType(typeId: 0)
class Contacts {
@HiveField(0)
String name;
@HiveField(1)
String imageURL;
@HiveField(2)
String? number;
Contacts({
required this.name = "some name",
required this.imageURL = "URL",
this.number = "425890",
});
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.