Flutter Hive - 未处理的异常:类型“List<dynamic>”不是类型转换中类型“List<SourceStations>”的子类型

Pin*_*rji 5 dart flutter flutter-dependencies flutter-hive

我正在使用这个包https://pub.dev/packages/hive

我想在配置单元中保存和检索自定义对象的列表。

我尝试过以下方法

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}'); 
Run Code Online (Sandbox Code Playgroud)

但我遇到了以下错误

E/flutter(24061):[错误:flutter/shell/common/shell.cc(199)] Dart错误:未处理的异常:E/flutter(24061):类型“List”不是类型“List”的子类型类型转换 E/flutter (24061): #0 BoxImpl.get (package:hive/src/box/box_impl.dart:43:26) E/flutter (24061): #1
_SourceToDestinationPageState.openStationBox

我已尝试检查解决方案,但没有足够的想法来解决此问题。

以下是我使用的hive版本

  • 蜂巢:^1.3.0
  • hive_flutter:^0.3.0+1
  • hive_generator:^0.7.0

小智 7

Box<List>“由于 Dart 的限制,不支持像这样的通用类型参数。” 作者文档中提到了这一点: 页面底部的https://docs.hivedb.dev/#/basics/boxes 。