[英]Flutter Hive - Unhandled exception: type 'List<dynamic>' is not a subtype of type 'List<SourceStations>' in type cast
I am using this package https://pub.dev/packages/hive我正在使用这个包https://pub.dev/packages/hive
I want to save and retrieve a list of custom objects in the hive.我想保存和检索配置单元中的自定义对象列表。
I have tried to below approach我试过下面的方法
await Hive.openBox<List<SourceStations>>(stationBox); //Open box
Box<List<SourceStations>> sourceStationsBox = Hive.box(stationBox);
sourceStationsBox.put(stationBox, listSourceStation); //Saving list of custom object as listSourceStation
//Should probably give lenght of list of custom object
logger.d('station box list length is ${sourceStationsBox.get(stationBox).length}');
But I am getting below error但我得到以下错误
E/flutter (24061): [ERROR:flutter/shell/common/shell.cc(199)] Dart Error: Unhandled exception: E/flutter (24061): type 'List' is not a subtype of type 'List' in type cast E/flutter (24061): #0 BoxImpl.get (package:hive/src/box/box_impl.dart:43:26) E/flutter (24061): #1
E/flutter (24061): [ERROR:flutter/shell/common/shell.cc(199)] Dart Error: Unhandled exception: E/flutter (24061): type 'List' is not a subtype of type 'List' in类型转换 E/flutter (24061): #0 BoxImpl.get (package:hive/src/box/box_impl.dart:43:26) E/flutter (24061): #1
_SourceToDestinationPageState.openStationBox_SourceToDestinationPageState.openStationBox
I have tried checking this solution but not getting enough idea how to fix this.我试过检查这个解决方案,但没有足够的想法如何解决这个问题。
Following are the hive version I am using以下是我正在使用的配置单元版本
"Generic type parameters like Box<List>
are unsupported due to Dart limitations." “由于 Dart 的限制,不支持像
Box<List>
这样的通用类型参数。” This is mentioned in the author documentation: https://docs.hivedb.dev/#/basics/boxes at the bottom of the page.这在作者文档中提到: https : //docs.hivedb.dev/#/basics/boxes在页面底部。
If you are not using a ValueListenableBuilder
, you can do it like this:如果您没有使用
ValueListenableBuilder
,您可以这样做:
await Hive.openBox<List<SourceStations>>(stationBox); //Open box
Box<List<SourceStations>> sourceStationsBox = Hive.box(stationBox);
sourceStationsBox.put(key, listSourceStation); // key is a string
logger.d('station box list length is ${sourceStationsBox.get(key).length}');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.