Hive Database - type 'String' is not a subtype of type 'HabitDatabase' in type cast

35 Views Asked by At

I want to put a "START_DATABASE" once new database is created. Here is the code below:

 createNewDatabase() async {
    habitsData = [
      HabitDatabase(
          progressTracker: 0,
          habitName: "Sample Habit 0",
          habitType: 0,
          habitQuestion: "Sample Question",
          habitTarget: 0,
          habitFrequency: "Sample Frequency",
          habitUnit: "Sample Unit")
    ];

    //databaseBox.put("habitList", "CURRENT_HABIT_LIST");

    //databaseBox.put("HABIT_LIST", habitsData);

    await databaseBox.put("START_DATE", todaysDateFormatted());
  }

I get this error: The following _TypeError was thrown building: type 'String' is not a subtype of type 'HabitDatabase' in type cast

I tried to add it in my HabitDataBase. The whole database.dart code is listed below:

import 'package:habit_tracker/datetime/date_time.dart';
import 'package:hive/hive.dart';
part 'habit_database.g.dart';

final databaseBox = Hive.box("habit_database");

@HiveType(typeId: 0)
class HabitDatabase {
  static List<HabitDatabase> habitsData = [];

  @HiveType(typeId: 0)
  int? progressTracker;

  @HiveType(typeId: 1)
  bool? habitFinished;

  @HiveType(typeId: 2)
  String? habitName;

  @HiveType(typeId: 3)
  int? habitType;

  @HiveType(typeId: 4)
  String? habitQuestion;

  @HiveType(typeId: 5)
  int? habitTarget;

  @HiveType(typeId: 6)
  String? habitFrequency;

  @HiveType(typeId: 7)
  String? habitUnit;

  @HiveType(typeId: 8)
  String? startDate;

  HabitDatabase({
    this.habitFinished,
    this.progressTracker,
    this.habitName,
    this.habitType,
    this.habitQuestion,
    this.habitTarget,
    this.habitFrequency,
    this.habitUnit,
    this.startDate,
  });

  @override
  String toString() {
    return 'HabitDatabase(progressTracker: $progressTracker, habitFinished: $habitFinished, habitName: $habitName, habitType: $habitType, habitQuestion: $habitQuestion, habitTarget: $habitTarget, habitFrequency: $habitFrequency, habitUnit: $habitUnit)';
  }

  createNewDatabase() async {
    habitsData = [
      HabitDatabase(
          progressTracker: 0,
          habitName: "Sample Habit 0",
          habitType: 0,
          habitQuestion: "Sample Question",
          habitTarget: 0,
          habitFrequency: "Sample Frequency",
          habitUnit: "Sample Unit")
    ];

    //databaseBox.put("habitList", "CURRENT_HABIT_LIST");

    //databaseBox.put("HABIT_LIST", habitsData);

    await databaseBox.put("START_DATE", todaysDateFormatted());
  }

  void loadData() {
    // if it's a new day, get habit list from database
    if (databaseBox.get(todaysDateFormatted()) == null) {
      habitsData = databaseBox.get("CURRENT_HABIT_LIST");
      // set all habit completed to false since it's a new day
      for (int i = 0; i < habitsData.length; i++) {
        habitsData[i].progressTracker = 0;
        habitsData[i].habitFinished = false;
      }
    }
    // if it's not a new day, load todays list
    else {
      habitsData = databaseBox.get(todaysDateFormatted());
    }
  }
}

this is the 'habit_database.g.dart'

// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'habit_database.dart';

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

class HabitDatabaseAdapter extends TypeAdapter<HabitDatabase> {
  @override
  final int typeId = 0;

  @override
  HabitDatabase read(BinaryReader reader) {
    final numOfFields = reader.readByte();
    final fields = <int, dynamic>{
      for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
    };
    if (fields.containsKey(0) && fields.containsKey(1)) {
      return HabitDatabase(
        progressTracker: fields[0] as int?,
        habitFinished: fields[1] as bool?,
        habitName: fields[2] as String?,
        habitType: fields[3] as int?,
        habitQuestion: fields[4] as String?,
        habitTarget: fields[5] as int?,
        habitFrequency: fields[6] as String?,
        habitUnit: fields[7] as String?,
        startDate: fields[8] as String?,
      );
    } else {
      // Handle the case where expected fields are not present
      throw HiveError('Invalid data format for HabitDatabase');
    }
  }

  @override
  void write(BinaryWriter writer, HabitDatabase obj) {
    writer.writeByte(10); // Number of fields

    // Write the fields to the binary writer
    writer.writeByte(0);
    writer.write(obj.progressTracker);

    writer.writeByte(1);
    writer.write(obj.habitFinished);

    writer.writeByte(2);
    writer.write(obj.habitName);

    writer.writeByte(3);
    writer.write(obj.habitType);

    writer.writeByte(4);
    writer.write(obj.habitQuestion);

    writer.writeByte(5);
    writer.write(obj.habitTarget);

    writer.writeByte(6);
    writer.write(obj.habitFrequency);

    writer.writeByte(7);
    writer.write(obj.habitUnit);

    writer.writeByte(8);
    writer.write(obj.startDate);
  }

  @override
  int get hashCode => typeId.hashCode;

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
      other is HabitDatabaseAdapter &&
          runtimeType == other.runtimeType &&
          typeId == other.typeId;
}

Here is the error

0

There are 0 best solutions below