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

casting Box<Map<String , dynamic>> to List<Map<String , dynamic >> throws _CastError #489

Closed
real-john-doe opened this issue Nov 13, 2020 · 3 comments
Assignees
Labels
problem An unconfirmed bug.

Comments

@real-john-doe
Copy link

real-john-doe commented Nov 13, 2020

Steps to Reproduce
1 _ Opening box with <Map<>String , dynamic> generic
2 _ adding some valid map to box using put method
3 _ reading box.values from box

Code sample

void main() async {
  await Hive.initFlutter();
  await Hive.openBox<Map<String, dynamic>>('activities');
}

Activity activity = Activity(
    id: 'uniqueId',
    name: 'Doing HomeWork',              
 );

box = Hive.box<Map<String, dynamic>>('activities');
box.put(activity.id, activity.toJson()); // works fine

box = Hive.box<Map<String, dynamic>>('activities');
List<Map<String, dynamic>> list = box.values.toList(); // throws  "type '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'Map<String, dynamic>' in type cast" after the first Hot restart or closing and opening application

// stacktarce info doesnt say much , but here it is

When the exception was thrown, this was the stack
#0      Keystore.getValues.<anonymous closure>package:hive/…/box/keystore.dart:122
#1      MappedIterator.moveNext (dart:_internal/iterable.dart:392:20)
#2      new List.from (dart:core-patch/array_patch.dart:50:19)
#3      new List.of (dart:core-patch/array_patch.dart:68:17)
#4      Iterable.toList (dart:core/iterable.dart:404:12)

Version

  • Platform: iOS, Android
  • Flutter version: 1.22.3 • channel stable
  • Hive version: 1.4.4+1
  • hive_flutter: 0.3.1
@real-john-doe real-john-doe added the problem An unconfirmed bug. label Nov 13, 2020
@themisir
Copy link
Contributor

Duplicate of #295

@themisir themisir marked this as a duplicate of #295 Nov 13, 2020
@real-john-doe
Copy link
Author

real-john-doe commented Nov 13, 2020

it isnt duplicate of 295 issue,
it doesnt work even after using both of these suggested workarounds

ps: i dont want to use an adapter

Unfortunately this is a limitation of the dart language. You can use

var box = await Hive.openBox<Map>('boxName');
var map = box.get("myMap").cast<String, dynamic>();

or you create an object which contains your box and generate a Hive adapter. The adapter will handle the casting.

Originally posted by @leisim in #295 (comment)

@2shrestha22
Copy link

2shrestha22 commented Aug 11, 2022

Open box with Map<dynamic, dynamic> and then cast it later.
Do it like this.

void main() async {
  await Hive.initFlutter();
  await Hive.openBox<Map<dynamic, dynamic>>('activities');
}

Activity activity = Activity(
    id: 'uniqueId',
    name: 'Doing HomeWork',              
 );

box = Hive.box<Map<dynamic, dynamic>>('activities');
box.put(activity.id, activity.toJson()); // works fine

box = Hive.box<Map<dynamic, dynamic>>('activities');
List<Map<String, dynamic>> list = box.values.map((e)=> e.cast<String, dynamic>()) .toList(); 

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

No branches or pull requests

4 participants