How to create a empty HiveList in constructor function? - flutter

I want to create an empty HiveList for my Hive class to make a relation with another Hive class. But I cannot make a default value at the constructor. I think I am doing this wrong. Unfortunately the Hive docs does not cover this issue.
#HiveType(typeId: 0)
class Account extends HiveObject {
#HiveField(0)
String name;
#HiveField(1)
int amount;
#HiveField(2)
HiveList<History> history; // Want to make this list
Account({
required this.name,
required this.amount,
this.history = [] as HiveList<History>, // Error Here
});
}

You should be able to do something like this:
#HiveType(typeId: 0)
class Account extends HiveObject {
#HiveField(0)
String name;
#HiveField(1)
int amount;
#HiveField(2)
HiveList<History> history;
Account({
required this.name,
required this.amount,
this.history = const HiveList<History>.empty(),
});
}

Related

Flutter Hive model have enum type but not able to put/set value in enum with hive-field

I want a hivemodel class which is contain enum as field but hive is not able to put set value.
CONTACTTYPE is enum:
#freezed
#HiveType(typeId: 7, adapterName: "FriendGroupEntityAdapter")
#freezed
class FriendsGroupEntity with _$FriendsGroupEntity {
const factory FriendsGroupEntity({
#HiveField(0) int userId,
#HiveField(1) #Default(CONTACTTYPE.loop) CONTACTTYPE contactType,
#HiveField(2) String contact,
#HiveField(3) int id,
#HiveField(4) int value,
#HiveField(5) int value2,
#HiveField(6) int value3,
#HiveField(7) int value7,
});
Here CONTACTTYPE.loop is an enum and I want to store it into hive, and got an error which I attach.
Is there any way for enum in hive?
Please follow this approach
import 'package:hive/hive.dart';
part 'myrepresentation.g.dart';
#HiveType(typeId: 1)
class FriendsGroupEntity extends HiveObject {
#HiveType(0)
final String id;
#HiveType(1)
final Foo foo;
MyRepresentation({required this.id, required this.foo});
}
#HiveType(typeId: 2)
enum Foo {
#HiveField(0)
foo,
#HiveField(1)
bar,
}
check this reference link

How to add a class in another one or an alternative

Hello I explain my problem I have a list of patients with their coordinates all that is registered in a class by clicking on one of the patient a life opens with the information of the patient in question.
I want to add the possibility to add notes (an unlimited number of notes per patient) so I need another list but I do not know how to link the two
I would like that when I click on a patient it loads the information of this patient from the list of notes
Here is the list of my patients
class Patients {
final String name;
final String firstname;
final String dateofbirth;
final String email;
final String numero;
final String id;
final DateTime date;
Patients(
{required this.name,
required this.firstname,
required this.dateofbirth,
required this.email,
required this.numero,
required this.id,
required this.date});
}
Here is the list of patient notes
class ListNote {
final String? title;
final String? note;
final String? conclusion;
ListNote({
this.title,
this.note,
this.conclusion,
});
}
My patient screen list
In the red frame is the information of my first list of patients.
In the blue boxes you will find the notes linked to the patient
Patient page with more info
Thank you for your help
What I got from your point is you want to add List of Notes in Patient class. If that's the case you can simply add that in Patient class like this:
class Patients {
final String name;
final List<ListNote> listOfNotes;
final String firstname;
final String dateofbirth;
final String email;
final String numero;
final String id;
final DateTime date;
Patients({
required this.name,
required this.listOfNotes,
required this.firstname,
required this.dateofbirth,
required this.email,
required this.numero,
required this.id,
required this.date
});
}
class ListNote {
final String? title;
final String? note;
final String? conclusion;
ListNote({
this.title,
this.note,
this.conclusion,
});
}

Dart json_serializable Cannot populate the required constructor argument. It is assigned to an ignored field

I'm writing a flutter app and i need to serialise a class to json. The class is :
#HiveType(typeId: 0)
#JsonSerializable()
class CartItem extends HiveObject {
#HiveField(1)
String item_id;
#HiveField(2)
String name;
#HiveField(3)
SelectionsList selections;
#HiveField(4)
String special_instructions;
#HiveField(5)
int quantity;
#HiveField(6)
double amount;
#HiveField(7)
#JsonKey(ignore: true)
MenuItem item;
CartItem(this.item_id, this.name, this.selections, this.quantity, this.special_instructions, this.amount, this.item);
}
however, due to the item field being ignored (intended), when i try to generate the .g.dart file, this error is thrown. Cannot populate the required constructor argument: item. It is assigned to an ignored field. I still need it to be in the constructor though, So is there a workaround for this?
So I managed to get this working. Not sure if it is the correct way but if it helps you out then great.
#HiveType(typeId: 0)
#JsonSerializable()
class CartItem extends HiveObject {
...
#HiveField(7)
#JsonKey(ignore: true)
MenuItem item;
CartItem(this.item_id,
this.name, this.selections, this.quantity, this.special_instructions, this.amount,
[this.item = MenuItem.empty()]);
}
You have to set the constructor entry an optional positional and give it a default value.
You can also make it nullable and leave out the default on the constructor item.

Do I need to create extra model for Hive TypeAdapter creation in flutter?

I have Expense Model,
class Expense {
String id;
String amount;
String details;
String type;
DateTime date;
ExpenseMode mode;
Expense(
{this.id, this.amount, this.details, this.type, this.date, this.mode});
}
and Hive Model as,
#HiveType(typeId: 1)
class Expense {
#HiveField(0)
String id;
#HiveField(1)
String amount;
#HiveField(2)
String details;
#HiveField(3)
String type;
#HiveField(4)
DateTime date;
#HiveField(5)
ExpenseMode mode;
Expense(
{this.id, this.amount, this.details, this.type, this.date, this.mode});
}
Can I use only Hype Model for Expense or need to keep separate Model for Hive TypeAdapter sepefic?
Hive does not require separate Hive Model specifically to create a Type Adapter, we can edit the existing model and and convert them to Hive Model and can create Type Adaptor from it.

Initialize object with final instances dart

If I have this class :
class Person {
Person(
#required this.name,
#required this.age,
#required this.job,
});
final String name;
final int age;
final String job;
);
How can I create an object that has all these properties but set by me before the use? Something like :
Person william(name : 'William', age : 25, job : 'officer');
doSomething(william);
Please see the code below :
void main() {
final Person william = Person(name:"William", age:25, job:"officer");
doSomething(william);
}
void doSomething(Person person) {
print('${person.name} ${person.age} ${person.job}');
}
class Person {
Person({
this.name,
this.age,
this.job,
});
final String name;
final int age;
final String job;
}