【问题标题】:Flutter Hive - Unhandled exception: type 'List<dynamic>' is not a subtype of type 'List<SourceStations>' in type castFlutter Hive - 未处理的异常:类型“List<dynamic>”不是类型转换中“List<SourceStations>”类型的子类型
【发布时间】:2020-06-28 23:19:38
【问题描述】:

我正在使用这个包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): [错误:flutter/shell/common/shell.cc(199)] Dart 错误:未处理的异常:E/flutter (24061): type 'List' is 不是类型转换 E/flutter 中“列表”类型的子类型 (24061): #0 BoxImpl.get (package:hive/src/box/box_impl.dart:43:26) E/flutter (24061): #1
_SourceToDestinationPageState.openStationBox

我已尝试检查 this 解决方案,但对如何解决此问题没有足够的了解。

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

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

【问题讨论】:

  • 你使用的是什么版本的飞镖?

标签: flutter dart flutter-dependencies flutter-hive


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    如果你没有使用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}'); 
    

    【讨论】:

      猜你喜欢
      • 2021-08-15
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 2022-12-03
      • 2022-01-13
      • 2021-08-03
      • 1970-01-01
      • 2021-09-16
      相关资源
      最近更新 更多