Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added a working inheritance example #40

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ To contribute, please open pull requests against the master branch.
You can run a local preview of the docs by running the following command in the project folder:

```
npm i docsify-cli -g
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
npm i docsify-cli -g

This is unnecessary, npx will install latest version temporarily.

npx docsify serve
```
1 change: 1 addition & 0 deletions _sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- [Generate adapter](custom-objects/generate_adapter.md)
- [HiveObject](custom-objects/hive_object.md)
- [Relationships](custom-objects/relationships.md)
- [Inheritance](custom-objects/inheritance.md)
- [Create adapter manually](custom-objects/create_adapter_manually.md)

- Flutter Tutorials
Expand Down
84 changes: 84 additions & 0 deletions custom-objects/inheritance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# Inheritance

Always register the base clase for last. Example


```dart
import 'package:hive/hive.dart';

part 'models.g.dart';

@HiveType(typeId: 0)
class Person extends HiveObject {
@HiveField(0)
List<Pet> pets = [];

@HiveField(1)
String name = "";

@HiveField(2)
Cat cat = Cat();

@HiveField(3)
Dog dog = Dog();

Person();
}

@HiveType(typeId: 1)
class Pet extends HiveObject {
@HiveField(0)
String name = "";
}

@HiveType(typeId: 2)
class Dog extends Pet {
@HiveField(1)
String someDogField = "";

Dog() : super();
}

@HiveType(typeId: 3)
class Cat extends Pet {
@HiveField(1)
String someCatField = "";

Cat() : super();
}


Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Hive.initFlutter();
Hive.registerAdapter<Person>(PersonAdapter());
Hive.registerAdapter<Dog>(DogAdapter());
Hive.registerAdapter<Cat>(CatAdapter());
Hive.registerAdapter<Pet>(PetAdapter()); # FOR LAST !!!

Box<Person> boxPersons = await Hive.openBox<Person>("persons");
if (boxPersons.values.isEmpty) {
print("Adding a new person");

Person person = Person()
..name = "person 1"
..pets = [
Pet()..name = "pet 1",
Pet()..name = "pet 2",
Pet()..name = "pet 3",
]
..dog = (Dog()
..name = "doggy"
..someDogField = "waw")
..cat = (Cat()
..name = "pussy"
..someCatField = "meau");

boxPersons.put(0, person);
} else {
print(boxPersons.values);
}

runApp(MyApp());
}
```