Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to read List<String> #33

Closed
bolasim opened this issue Sep 2, 2019 · 10 comments
Closed

Unable to read List<String> #33

bolasim opened this issue Sep 2, 2019 · 10 comments
Assignees
Labels
bug Something isn't working

Comments

@bolasim
Copy link

bolasim commented Sep 2, 2019

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;
}
@bolasim bolasim added the bug Something isn't working label Sep 2, 2019
@Afsar-Pasha
Copy link
Contributor

From the docs

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

@bolasim
Copy link
Author

bolasim commented Sep 2, 2019

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

@bolasim
Copy link
Author

bolasim commented Sep 2, 2019

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?

@simc
Copy link
Member

simc commented Sep 2, 2019

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

@bolasim
Copy link
Author

bolasim commented Sep 2, 2019

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.

@simc
Copy link
Member

simc commented Sep 2, 2019

Thanks, I'll investigate that.

Edit: Which hive version are you using?

@simc
Copy link
Member

simc commented Sep 3, 2019

Could you please share the output of hive_generator?

@bolasim
Copy link
Author

bolasim commented Sep 3, 2019

// 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);
  }
}

@bolasim
Copy link
Author

bolasim commented Sep 4, 2019

Hive version:

Hive

hive: ^0.5.0

@simc
Copy link
Member

simc commented Sep 5, 2019

This is fixed with hive_generator: 0.4.0+2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants