Comments (6)
Yes it is possible but as you said, Hive would need to understand the constructors.
I don't think this is a very hard thing to implement but the generator may need some changes. This is definitely a planned feature but I'm currently working on other features and bugfixes.
Another important thing that is missing are unit tests for the generator. I'm not sure how to do that ^^
I would appreciate your help if you want to to implement the constructor feature ;)
from hive.
Hmm, the more I think about this, the more complex this feature seems.
Like, theoretically, the classes can be complex, mixing final and non-final fields and altering them in the constructor.
For example, consider the following class:
@HiveType()
class Label {
@HiveField(0)
final String name;
@HiveField(1)
String author;
Label();
Label.withLowercaseName(String name) : this.name = name.toLowercase();
Label.withName(this.name);
}
Theoretically, hive could be clever enough to come up with would be something of the likes:
return SampleClass.withName(name)
..author = author;
But that seems complex, especially considering that attributes can be named, positioned or required.
There's just a lot of variation going on in constructors.
So I'm afraid the code generator would become very complex.
Another option would be to limit the constructors that are accepted, maybe even requiring a fromHive
constructor for classes that contain final fields, something like this:
@HiveType()
class Label {
...
Label.fromHive({
this.name,
this.author
});
}
Any ideas on how to proceed?
What do you think about requiring a special constructor for classes with final fields that still want to be stored in hive?
from hive.
Quick sidenote: For my use case I just created a package data_classes
that can convert a mutable class from and to an immutable class pendant.
So you could define a mutable class in a few lines of code and annotate it with @HiveType
. An immutable class gets generated that can be used everywhere else in the app. When you want to save such a class to hive, you can convert it to the mutable class and save this to hive. Vice versa for retrieving objects from hive.
from hive.
I think it is not that complex to make it work with named and positioned parameters. The only thing I would require is that the constructor uses the this.myAttribute
initialization.
A constructor may look like this:
class MyClass {
String name;
int age;
MyClass(this.age, {this.name});
The data_classes
package is really cool...
from hive.
Okay, so we use the unnamed constructor and set only those fields that are assigned with this.field
.
Just to be sure, let's say we have a class
@HiveType()
class MyClass {
@HiveField(0)
final String name;
@HiveField(1)
int age;
@HiveField(2)
int fieldThatIsNotInTheConstructor;
MyClass(this.age, {this.name});
}
I'll try to implement the generator so that the generated code could then look like this:
MyClass read(BinaryReader reader) {
var numOfFields = reader.readByte();
var fields = <int, dynamic>{
for (var i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
};
return MyClass(
fields[1] as int,
name: fields[0] as String,
)..fieldThatIsNotInTheConstructor = fields[2] as int;
}
from hive.
Yes that looks good π
from hive.
Related Issues (20)
- Question: Read .hive files without using HiveDB functions
- Wrong Pub dev documentation
- Future of Hive/Isar HOT 2
- Github link missing in package listing on pub.dev
- Support for record types
- [Web] The "values" ββreturned in "Box" are empty HOT 1
- Fail to initialize Hive 4.0.0 for Flutter Web? HOT 1
- [Hive v4] Type mistmatch when get List<T> items.
- Unable to install on new flutter project HOT 2
- Support for enhanced enums
- Updating nested map keys
- Is the hive dead?
- Is there a way to use the map type adapter on a nested class using toJson?
- [Web] Issue with "Invalid argument(s): Invalid or corrupted pad block" in Flutter HOT 4
- Unhandled Exception: Out of Memory
- Flutter hive unit test HOT 1
- TypeId is not consistent and how to safely remove adapter
- iOS only: hive with encryption clearing database, app includes db usage in background HOT 2
- Wrong naming of hive constant global variable
- New data written to the disk is not immediately available. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from hive.