4

我正在使用这个包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}'); 

但我得到以下错误

E/flutter (24061): [ERROR:flutter/shell/common/shell.cc(199)] Dart 错误: 未处理的异常: 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

我试过检查这个解决方案,但没有足够的想法来解决这个问题。

以下是我正在使用的配置单元版本

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

2 回答 2

4

"Generic type parameters like Box<List> are unsupported due to Dart limitations." This is mentioned in the author documentation: https://docs.hivedb.dev/#/basics/boxes at the bottom of the page.

于 2020-04-04T11:08:39.323 回答
-1

如果你不使用 a 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}'); 
于 2020-09-11T19:40:18.647 回答