Error with Set in generated file
- Dominant language
- Dart
- Stars
- 4.4k
- Forks
- 449
- PR merge metrics
- No merged PRs in 30d
Description
**Steps to Reproduce**
When I try to generate flashcards_model.g.dart, it'll be generate with error
import 'package:hive/hive.dart';
part 'flashcards_model.g.dart';
generate via
`flutter pub run build_runner build --delete-conflicting-outputs`
Generation from this code sample:
```
@HiveType(typeId: 1)
class FlashCard {
@HiveField(0)
String fromLanguage;
@HiveField(1)
String toLanguage;
@HiveField(2)
String questionWords;
@HiveField(3)
String answerWords;
@HiveField(4)
DateTime lastTested;
@HiveField(5)
DateTime nextTest;
@HiveField(6)
int correctAnswers;
@HiveField(7)
int wrongAnswers;
@HiveField(8)
bool isDeleted;
FlashCard(
{required this.fromLanguage,
required this.toLanguage,
required this.questionWords,
required this.answerWords,
required this.lastTested,
required this.nextTest,
required this.correctAnswers,
required this.wrongAnswers,
required this.isDeleted});
/// returns true if the answer was correct, change correct and wrong answers and delay the next test date
bool answeredCorrectly() {
correctAnswers = correctAnswers + 1;
wrongAnswers = wrongAnswers - 1 != 0 ? wrongAnswers - 1 : 0;
delayTestDate(correctAnswers);
return true;
}
/// returns true if the answer was wrong, change correct and wrong answers and delay the next test date to today
bool answeredWrong() {
wrongAnswers = wrongAnswers + 1;
correctAnswers = correctAnswers - 1 != 0 ? correctAnswers - 1 : 0;
// delay test date by today for wrong answers
delayTestDate(0);
return true;
}
/// returns true if the test date was delayed, change the next test date
bool delayTestDate(int days) {
lastTested = DateTime.now();
nextTest = nextTest.add(Duration(days: days));
return true;
}
@override
String toString() {
return 'FlashCard{fromLanguage: $fromLanguage, toLanguage: $toLanguage, questionWords: $questionWords, answerWords: $answerWords, lastTested: $lastTested, nextTest: $nextTest, correctAnswers: $correctAnswers, wrongAnswers: $wrongAnswers, isDeleted: $isDeleted}';
}
@override
int get hashCode =>
fromLanguage.hashCode ^
toLanguage.hashCode ^
questionWords.hashCode ^
answerWords.hashCode;
/// returns true if the hashcodes are the same
bool hashCheck(FlashCard other) => other.hashCode == hashCode;
/// returns true if the words are the same
bool wordCheck(FlashCard other) =>
questionWords + answerWords == other.questionWords + other.answerWords;
/// returns true if the words are the same but reversed
bool reverseWordCheck(FlashCard other) =>
(questionWords + answerWords == other.answerWords + other.questionWords);
/// returns true if the languages are the same
bool languageCheck(FlashCard other) =>
fromLanguage == other.fromLanguage && toLanguage == other.toLanguage;
/// returns true if the languages are the same but reversed
bool reversedLanguageCheck(FlashCard other) =>
fromLanguage == other.toLanguage && toLanguage == other.fromLanguage;
/// returns true if the words same or reversed words are the same
bool fullWordCheck(FlashCard other) =>
wordCheck(other) || reverseWordCheck(other);
/// returns true if the languages are the same or reversed languages are the same
bool fullLanguageCheck(FlashCard other) =>
languageCheck(other) || reversedLanguageCheck(other);
@override
bool operator ==(Object other) {
// check if the hashcodes are the same, return true if they are
bool fastCheck = other is FlashCard && (hashCheck(other));
if (fastCheck) return true;
bool slowCheck = other is FlashCard &&
(fullWordCheck(other) && fullLanguageCheck(other));
return slowCheck;
}
}
@HiveType(typeId: 2)
class FlashCardCollection {
FlashCardCollection(this.id,
{required this.title,
required this.flashCardSet,
required this.createdAt});
@HiveField(0)
String id;
@HiveField(1)
String title;
@HiveField(2)
Set flashCardSet;
@HiveField(3)
DateTime createdAt;
@override
String toString() {
return 'FlashCardCollection{title: $title, flashCards: $flashCardSet , createdAt: $createdAt}';
}
static List sortedByDate(
Set setFlashcards) {
List flist = setFlashcards.toList();
flist.sort((a, b) => b.createdAt.compareTo(a.createdAt));
return flist;
}
@override
int get hashCode => id.hashCode;
@override
bool operator ==(Object other) {
return other is FlashCardCollection && other.id == id;
}
}
```
**Generated with error code sample**
```
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'flashcards_model.dart';
// **************************************************************************
// TypeAdapterGenerator
// **************************************************************************
class FlashCardAdapter extends TypeAdapter {
@override
final int typeId = 1;
@override
FlashCard read(BinaryReader reader) {
final numOfFields = reader.readByte();
final fields = {
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
};
return FlashCard(
fromLanguage: fields[0] as String,
toLanguage: fields[1] as String,
questionWords: fields[2] as String,
answerWords: fields[3] as String,
lastTested: fields[4] as DateTime,
nextTest: fields[5] as DateTime,
correctAnswers: fields[6] as int,
wrongAnswers: fields[7] as int,
isDeleted: fields[8] as bool,
);
}
@override
void write(BinaryWriter writer, FlashCard obj) {
writer
..writeByte(9)
..writeByte(0)
..write(obj.fromLanguage)
..writeByte(1)
..write(obj.toLanguage)
..writeByte(2)
..write(obj.questionWords)
..writeByte(3)
..write(obj.answerWords)
..writeByte(4)
..write(obj.lastTested)
..writeByte(5)
..write(obj.nextTest)
..writeByte(6)
..write(obj.correctAnswers)
..writeByte(7)
..write(obj.wrongAnswers)
..writeByte(8)
..write(obj.isDeleted);
}
@override
int get hashCode => typeId.hashCode;
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is FlashCardAdapter &&
runtimeType == other.runtimeType &&
typeId == other.typeId;
}
class FlashCardCollectionAdapter extends TypeAdapter {
@override
final int typeId = 2;
@override
FlashCardCollection read(BinaryReader reader) {
final numOfFields = reader.readByte();
final fields = {
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
};
return FlashCardCollection(
fields[0] as String,
title: fields[1] as String,
flashCardSet: (fields[2] as List).cast(),
createdAt: fields[3] as DateTime,
);
}
@override
void write(BinaryWriter writer, FlashCardCollection obj) {
writer
..writeByte(4)
..writeByte(0)
..write(obj.id)
..writeByte(1)
..write(obj.title)
..writeByte(2)
..write(obj.flashCardSet.toList())
..writeByte(3)
..write(obj.createdAt);
}
@override
int get hashCode => typeId.hashCode;
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is FlashCardCollectionAdapter &&
runtimeType == other.runtimeType &&
typeId == other.typeId;
}
```
**Version**
- Platform: Android
- Flutter version: 3.7.8
- Hive version: hive: ^2.2.3
- VScode
- Ubuntu 22.04
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with the flashcards_model.dart declarations and the generated flashcards_model.g.dart output, then run flutter pub run build_runner build --delete-conflicting-outputs using the supplied FlashCardCollection example. Compare the generated adapter's handling of flashCardSet with its declared Set type and confirm generation completes without an error and preserves the collection data.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- flutter
- Domain
- database
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100