how to solve dynamic list type casting error in Hive?

432

Solution 1

Is not necessary to open your box as a List, because it is a box and can store many objects of the type that you declare, for example:

await Hive.openBox<MyModel>(boxName);

To get all the objects or data stored in that box, you can query like this:

final box = Hive.box<CompanyModel>(boxName);
List<CompanyModel> interviews = box.values.toList();

In addition, you have to create an Adapter Model if you want to store your own Model with Hive.

There is two dev dependencies to auto generate the Model:

dev_dependencies: 
  hive_generator: 
  build_runner: 

Importing that dependencies and running this command flutter packages pub run build_runner build will generate the Model, but also you have to create your Model as the documentation indicates.

I suggest you to check out the documentation.

Hive - Generate Adapter

Solution 2

I can finally solve it by using it like this. in main function

Future<void> main() async {

  await Hive.initFlutter();
  await Hive.openBox("events");

}

when saving data list

final eventsBox = Hive.box("events");
eventsBox.put("recommended_events", eventsFromServer);

and read it like this

final eventsBox = Hive.box("events");
final eventsFromHive = eventsBox.get("recommended_events")?.cast<Event>() ?? [];
Share:
432
Alexa289
Author by

Alexa289

Updated on December 30, 2022

Comments

  • Alexa289
    Alexa289 over 1 year

    sorry I am new in using Flutter and using Hive local storage.

    I am using

      hive: ^2.0.4
      hive_flutter: ^1.0.0
    

    I open the box in main function like this

    Future<void> main() async {
    
      await Hive.initFlutter();
      await Hive.openBox<List<Event>>("events");
    
    }
    

    after getting the data from the server, I save all the events to hive by using code like this

    final eventsBox = Hive.box<List<Event>>("events");
    final List<Event> eventsFromServer = await getEventsFromServer();
    eventsBox.put("recommended_events", eventsFromServer);
    

    but I have error when trying to read the data from the box, I read it like this

    final eventsBox = Hive.box<List<Event>>("events");
    
    // error in this one line below
    final eventsFromHive = eventsBox.get("recommended_events", defaultValue: []) ?? []; 
    

    type 'List < dynamic > ' is not a subtype of type 'List< Event >?' in type cast

    how to solve this type casting error?

    from the documentation in here it is said

    Lists returned by get() are always of type List (Maps of type Map<dynamic, dynamic>). Use list.cast() to cast them to a specific type.

    I don't know if it is the solution of my problem or not, but I don't know how to implement that in my code.

    I tried it like this, but I still have the same error

    final eventsFromHive = eventsBox.get("recommended_events")!.cast<Event>();
    

    or maybe the way I write the syntax to save and read the list are totally wrong? please help :)

    • Lakmal Fernando
      Lakmal Fernando almost 3 years
      final eventsFromHive = eventsBox.get("recommended_events", defaultValue: <Event>[]) ?? [];
    • Alexa289
      Alexa289 almost 3 years
      @sajithlakmal unfortunately it doesn't work :(
    • Lakmal Fernando
      Lakmal Fernando almost 3 years
      What is you Event object
    • sungkd123
      sungkd123 almost 3 years
      Can you please post your events model class definition