'how to solve dynamic list type casting error in Hive?

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 :)



Solution 1:[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:[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>() ?? [];

Solution 3:[3]

I have faced this kind of problem. It was absolutely the same. I do not know how you got kinda problem. Maybe it was the same with mine. I have just cleaned the box. and it has worked on me.

**Cause: **

I started it immediately after I made the box (for testing). he had taken the List<dynamic> object as it store. Once I made it clear, it stocked up data I had just given and it worked

Try:

boxName.clear() inside initState() and re-run it. if it will work do not forget to delete the line!

if you forget, it will clear the box every time.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jesus Coronado
Solution 2 Alexa289
Solution 3