I'm trying to store a Map<String, int>() in the hive database. I'm able to store but I'm not able to retrieve the Map() it prompts the error
Unhandled Exception: type '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'Map<String, int>?' in type cast
Code:
Data Controller
Box<Map<String, int>> hiveBox = Hive.box('SaveBetBox');
void betStorage({String? matchKey, String? statKey, int? statIndex}) {
// Checks if the hive box has the data with following key if not adds
hiveBox.containsKey(matchKey)
? null
: hiveBox.put(
matchKey,
Map<String, int>(),
);
// Extract hive map to update or add Key values to Map with specific Key
Map<String, int>? tm = hiveBox.get(matchKey);
if (tm![statKey!] == null) {
tm[statKey] = statIndex!;
hiveBox.put(matchKey, tm);
} else {
tm.update(statKey, (value) => statIndex!);
}
Map<String, int>? dd = hiveBox.get(matchKey);
print('This is $statKey ${dd![statKey]}');
}
// Retrieve FN
void onitintHive() {
//Storing value i.e Map() of specific key in var
Map<String, int>? _dd = hiveBox.get("RVB");
// Extracting values from the map
int? goal1 = _dd!['t1goalkey'];
int? yc = _dd['t1yckey'];
print('This is dd : ${_dd!['t1goalkey']}');
print('This is T1 Goal : $goal1');
print('This is T1 YC : $yc');
}
Main Box in main()
await Hive.openBox<Map<String, int>>('SaveBetBox');
I solved the problem. I might be wrong but seems like Hive Box return only maps with dynamic types, i.e Map<dynamic, dynamic>(). The error occurred because I was trying to fit Map<dynamic, dynamic>() to a Map<String, int>(). Changing the receiver Map to Map<dynamic, dynamic>() solved the issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With