Hive: Unable to read List<String>

Created on 2 Sep 2019  路  10Comments  路  Source: hivedb/hive

I keep getting the following error from the autogenerated adapter.

I/flutter (30530): type 'List<dynamic>' is not a subtype of type 'List<String>'

My schema is below. The put works fine. It errors when I call get.

import 'package:hive/hive.dart';

part 'chapter.g.dart';

@HiveType()
class Chapter {
  Chapter(
      {this.number,
      this.numVerses,
      this.numSections,
      this.sectionTitles,
      this.sectionStartVerses,
      this.verses});

  @HiveField(0)
  int number;

  @HiveField(1)
  int numVerses;

  @HiveField(2)
  int numSections;

  @HiveField(3)
  List<String> sectionTitles;

  @HiveField(4)
  List<int> sectionStartVerses;

  @HiveField(5)
  List<String> verses;
}
bug

All 10 comments

From the docs

Lists returned by get() are always of type List (Maps of type Map). Use list.cast() to cast them to a specific type.

Do you have an example? I tried casting in the read function itself and it still fails.

I got it to work by changing the read code in the generator to the following:

        case 3:
          obj.sectionTitles =
              reader.read()?.cast<String>();

Would it be worth changing the generator to have this as the default case for processing list in Adapters?

Which version of hive_generator are you using? The newest version should cast automatically...

hive_generator: ^0.4.0+1

It was trying to cast like this


          obj.verses = reader.read()?.map((dynamic e) => e as String)?.toList();

but would still throw the same error.

Thanks, I'll investigate that.

Edit: Which hive version are you using?

Could you please share the output of hive_generator?

// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'chapter.dart';

// **************************************************************************
// TypeAdapterGenerator
// **************************************************************************

class ChapterAdapter extends TypeAdapter<Chapter> {
  @override
  Chapter read(BinaryReader reader) {
    var obj = Chapter();
    var numOfFields = reader.readByte();
    for (var i = 0; i < numOfFields; i++) {
      switch (reader.readByte()) {
        case 0:
          obj.number = reader.read() as int;
          break;
        case 1:
          obj.numVerses = reader.read() as int;
          break;
        case 2:
          obj.numSections = reader.read() as int;
          break;
        case 3:
          obj.sectionTitles =
              reader.read()?.map((dynamic e) => e as String)?.toList();
          break;
        case 4:
          obj.sectionStartVerses =
              reader.read()?.map((dynamic e) => e as int)?.toList();
          break;
        case 5:
          obj.verses = reader.read()?.map((dynamic e) => e as String)?.toList();
          break;
      }
    }
    return obj;
  }

  @override
  void write(BinaryWriter writer, Chapter obj) {
    writer.writeByte(6);
    writer.writeByte(0);
    writer.write(obj.number);
    writer.writeByte(1);
    writer.write(obj.numVerses);
    writer.writeByte(2);
    writer.write(obj.numSections);
    writer.writeByte(3);
    writer.write(obj.sectionTitles);
    writer.writeByte(4);
    writer.write(obj.sectionStartVerses);
    writer.writeByte(5);
    writer.write(obj.verses);
  }
}

Hive version:

# Hive
hive: ^0.5.0

This is fixed with hive_generator: 0.4.0+2

Was this page helpful?
0 / 5 - 0 ratings

Related issues

abacaj picture abacaj  路  3Comments

cachapa picture cachapa  路  4Comments

yaymalaga picture yaymalaga  路  4Comments

azilvl picture azilvl  路  3Comments

Hopheylalal picture Hopheylalal  路  4Comments