Skip to content

simple inheritance broken #664

@sebastianovide

Description

@sebastianovide

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]

Metadata

Metadata

Assignees

No one assigned

    Labels

    problemAn unconfirmed bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions