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

simple inheritance broken #664

Open
sebastianovide opened this issue May 16, 2021 · 1 comment · May be fixed by hivedb/docs#40
Open

simple inheritance broken #664

sebastianovide opened this issue May 16, 2021 · 1 comment · May be fixed by hivedb/docs#40
Labels
problem An unconfirmed bug.

Comments

@sebastianovide
Copy link

sebastianovide commented May 16, 2021

Steps to Reproduce
Use the model and code below. Refresh the page and you will get this error:

    Restarted application in 104ms.
Error: Expected a value of type 'Cat', but got one of type 'Pet'
    at Object.throw_ [as throw] (http://localhost:51836/dart_sdk.js:5031:11)
    at Object.castError (http://localhost:51836/dart_sdk.js:5004:15)
    at Object.cast [as as] (http://localhost:51836/dart_sdk.js:5313:17)
    at Function.as_C [as as] (http://localhost:51836/dart_sdk.js:4950:19)
    at http://localhost:51836/packages/bug_repport/models.dart.lib.js:172:29
    at models.PersonAdapter.new.read (http://localhost:51836/packages/bug_repport/models.dart.lib.js:175:9)
    at binary_reader_impl.BinaryReaderImpl.new.read (http://localhost:51836/packages/hive/src/box/lazy_box_impl.dart.lib.js:1671:35)
    at storage_backend_js.StorageBackendJs.new.decodeValue (http://localhost:51836/packages/hive/src/box/lazy_box_impl.dart.lib.js:1802:27)
    at MappedListIterable.new.elementAt (http://localhost:51836/dart_sdk.js:21831:25)
    at ListIterator.new.moveNext (http://localhost:51836/dart_sdk.js:21637:55)
    at JsIterator.next (http://localhost:51836/dart_sdk.js:6699:21)
    at storage_backend_js.StorageBackendJs.new.initialize (http://localhost:51836/packages/hive/src/box/lazy_box_impl.dart.lib.js:1876:20)
    at initialize.next (<anonymous>)
    at http://localhost:51836/dart_sdk.js:37209:33
    at _RootZone.runUnary (http://localhost:51836/dart_sdk.js:37080:59)
    at _FutureListener.thenAwait.handleValue (http://localhost:51836/dart_sdk.js:32336:29)
    at handleValueCallback (http://localhost:51836/dart_sdk.js:32863:49)
    at Function._propagateToListeners (http://localhost:51836/dart_sdk.js:32901:17)
    at _Future.new.[_completeWithValue] (http://localhost:51836/dart_sdk.js:32749:23)
    at async._AsyncCallbackEntry.new.callback (http://localhost:51836/dart_sdk.js:32770:35)
    at Object._microtaskLoop (http://localhost:51836/dart_sdk.js:37332:13)
    at _startMicrotaskLoop (http://localhost:51836/dart_sdk.js:37338:13)
    at http://localhost:51836/dart_sdk.js:33109:9
Error: Expected a value of type 'Cat', but got one of type 'Pet'
    at Object.throw_ [as throw] (http://localhost:51836/dart_sdk.js:5031:11)
    at Object.castError (http://localhost:51836/dart_sdk.js:5004:15)
    at Object.cast [as as] (http://localhost:51836/dart_sdk.js:5313:17)
    at Function.as_C [as as] (http://localhost:51836/dart_sdk.js:4950:19)
    at http://localhost:51836/packages/bug_repport/models.dart.lib.js:172:29
    at models.PersonAdapter.new.read (http://localhost:51836/packages/bug_repport/models.dart.lib.js:175:9)
    at binary_reader_impl.BinaryReaderImpl.new.read (http://localhost:51836/packages/hive/src/box/lazy_box_impl.dart.lib.js:1671:35)
    at storage_backend_js.StorageBackendJs.new.decodeValue (http://localhost:51836/packages/hive/src/box/lazy_box_impl.dart.lib.js:1802:27)
    at MappedListIterable.new.elementAt (http://localhost:51836/dart_sdk.js:21831:25)
    at ListIterator.new.moveNext (http://localhost:51836/dart_sdk.js:21637:55)
    at JsIterator.next (http://localhost:51836/dart_sdk.js:6699:21)
    at storage_backend_js.StorageBackendJs.new.initialize (http://localhost:51836/packages/hive/src/box/lazy_box_impl.dart.lib.js:1876:20)
    at initialize.next (<anonymous>)
    at http://localhost:51836/dart_sdk.js:37209:33
    at _RootZone.runUnary (http://localhost:51836/dart_sdk.js:37080:59)
    at _FutureListener.thenAwait.handleValue (http://localhost:51836/dart_sdk.js:32336:29)
    at handleValueCallback (http://localhost:51836/dart_sdk.js:32863:49)
    at Function._propagateToListeners (http://localhost:51836/dart_sdk.js:32901:17)
    at _Future.new.[_completeWithValue] (http://localhost:51836/dart_sdk.js:32749:23)
    at async._AsyncCallbackEntry.new.callback (http://localhost:51836/dart_sdk.js:32770:35)
    at Object._microtaskLoop (http://localhost:51836/dart_sdk.js:37332:13)
    at _startMicrotaskLoop (http://localhost:51836/dart_sdk.js:37338:13)
    at http://localhost:51836/dart_sdk.js:33109:9

Code sample

models.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();
}

main

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Hive.initFlutter();
  Hive.registerAdapter<Person>(PersonAdapter());
  Hive.registerAdapter<Pet>(PetAdapter());
  Hive.registerAdapter<Dog>(DogAdapter());
  Hive.registerAdapter<Cat>(CatAdapter());
  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());
}

Version

  • Platform: Web
  • Flutter version: [2.0.6]
  • Hive version: [2.0.4]
@sebastianovide sebastianovide added the problem An unconfirmed bug. label May 16, 2021
@sebastianovide sebastianovide linked a pull request May 16, 2021 that will close this issue
thirdbody pushed a commit to thirdbody/hive that referenced this issue Jul 15, 2021
Resolves error "The argument type 'HiveList<T>?' can't be assigned to the parameter type 'HiveList<T>'.

Closes issue isar#664.
@thirdbody
Copy link

My mistake, referenced the wrong issue by accident.

themisir added a commit that referenced this issue Jul 15, 2021
* Replaced ?. operator with . operator

Resolves error "The argument type 'HiveList<T>?' can't be assigned to the parameter type 'HiveList<T>'.

Closes issue #664.

* Ensuring nullable HiveLists are properly generated

Co-authored-by: Misir Jafarov <misir.ceferov@gmail.com>
mrdavidrees pushed a commit to mrdavidrees/hive that referenced this issue Jan 16, 2022
Ensure HiveLists are treated as non-nullable in hive_generator (isar#728)

* Replaced ?. operator with . operator

Resolves error "The argument type 'HiveList<T>?' can't be assigned to the parameter type 'HiveList<T>'.

Closes issue isar#664.

* Ensuring nullable HiveLists are properly generated

Co-authored-by: Misir Jafarov <misir.ceferov@gmail.com>

Updated hive_generator dependencies (isar#765)

* Updated dependencies

* Changed version dependencies

Bump up hive_generator to v1.1.1

Get indexDB selectively based on window property (isar#802)

Co-authored-by: Wenhao Wu <wenhao.wu@signanthealth.com>

Add missing path parameter to HiveInterface methods (isar#776)

Fixes broken CI (isar#806)

* Update pubspec.yaml

* Migrated from mockito to mocktail

* Test on dart 2.14

Don't loose track of box objects if init crashes (isar#846)

feat: Add flush method to boxes (isar#852)

bump: Update to v2.0.5

Updated hive_generator analyzer dependency (isar#858)

bump up hive_generator to v1.1.2

Change pedantic to linter and fix warnings (isar#876)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
problem An unconfirmed bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants