Hive: Error running TypeAdapterGenerator Only classes or enums are allowed to be annotated with @HiveType.

Created on 4 Jul 2020  路  7Comments  路  Source: hivedb/hive

Steps to Reproduce
Running flutter pub run build_runner build --delete-conflicting-outputs with the following model.

Code sample

(I have also tried moving the enums to their own files, with the same error)

import 'package:flutter/foundation.dart';
import 'package:hive/hive.dart';

part 'uploadJob.g.dart'; /* client type generator for Hive */

/// Class
@HiveType(typeId: 4)
class UploadJob extends HiveObject {
  @HiveField(0)
  final int startedTimestamp;

  @HiveField(1)
  UploadStatus uploadStatus;

  @HiveField(2)
  List<String> uploadStatusMessages;

  @HiveField(3)
  final int totalItems;

  @HiveField(4)
  int itemUploadsCompleted;

  @HiveField(5)
  UploadDestination uploadDestination;

  @HiveField(6)
  final String uploadUserEmail;

  @HiveField(7)
  final String uploadUserId;

  UploadJob({
    @required this.startedTimestamp,
    this.uploadStatus = UploadStatus.not_started,
    this.uploadStatusMessages,
    this.totalItems,
    this.itemUploadsCompleted,
    this.uploadDestination,
    this.uploadUserEmail,
    this.uploadUserId,
  });
}

/// Enums
// Possible upload statuses
@HiveType(typeId: 41)
enum UploadStatus {
  @HiveField(0)
  not_started,
  @HiveField(1)
  in_progress,
  @HiveField(2)
  failed,
  @HiveField(3)
  success,
  @HiveField(4)
  cancelled,
}

// Possible upload destinations
@HiveType(typeId: 42)
enum UploadDestination {
  @HiveField(0)
  local_dump,
  @HiveField(1)
  firestore,
}

Version

  • Platform: Android Studio Mac
  • Flutter version: v1.17.5
  • Hive version:
dependencies:
  flutter:
    sdk: flutter

  hive: ^1.4.1+1
  hive_flutter: ^0.3.0+2
  random_string: ^1.1.0
  path_provider: ^1.4.0
  provider: ^4.0.1
  fluttertoast: ^3.1.3
  intl: ^0.16.0
  flutter_sound: ^2.0.3
  permission_handler: ^5.0.1+1
  image_picker: ^0.6.5
  firebase_auth: ^0.16.0
  firebase_core: ^0.4.4+3
  cloud_firestore: ^0.13.6
  data_connection_checker: ^0.3.4

dev_dependencies:
  flutter_test:
    sdk: flutter

  hive_generator: ^0.7.0+2
  build_runner: ^1.10.0

dependency_overrides:
  dartx: ^0.3.0
problem

Most helpful comment

I submitted a PR above for a fix, but for now I'm using my own forked repo for the hive_generator, under dev_dependencies:

dev_dependencies:
  flutter_test:
    sdk: flutter

  hive_generator:
    git:
      url: https://github.com/mdrideout/hive
      path: hive_generator
  build_runner: ^1.10.0

Will be switching back to @leisim's package once the fix is enabled.

All 7 comments

Potentialy the same issue as https://github.com/hivedb/hive/issues/361

Got the same.

  • Platform: Android Studio Mac
  • Flutter version: v1.17.5

Dependencies:

dependency_overrides:
  dartx: ^0.4.2

dependencies:
  flutter:
    sdk: flutter

  cupertino_icons: ^0.1.3

  google_sign_in: ^4.5.1
  firebase_auth: ^0.16.1

  injectable: ^0.4.0+1
  get_it: ^4.0.2
  flutter_secure_storage: ^3.3.3
  flutter_dotenv: ^2.1.0
  graphql_flutter: ^3.0.1
  font_awesome_flutter: ^8.8.1
  google_fonts: ^1.1.0
  url_launcher: ^5.4.11
  mobx: ^1.2.1+1
  flutter_mobx: ^1.1.0+1
  hive: ^1.4.1+1
  hive_flutter: ^0.3.0+2
  path_provider: ^1.6.11

dev_dependencies:
  flutter_test:
    sdk: flutter
  build_runner: ^1.10.0
  mobx_codegen: ^1.1.0+1
  injectable_generator: ^0.4.1
  hive_generator: ^0.7.0+2

I submitted a PR above for a fix, but for now I'm using my own forked repo for the hive_generator, under dev_dependencies:

dev_dependencies:
  flutter_test:
    sdk: flutter

  hive_generator:
    git:
      url: https://github.com/mdrideout/hive
      path: hive_generator
  build_runner: ^1.10.0

Will be switching back to @leisim's package once the fix is enabled.

I encountered this problem when the package analyzer updated from 0.39.10 to 0.39.11, and could temporary fix it by setting the analyzer version.

I encountered this problem when the package analyzer updated from 0.39.10 to 0.39.11, and could temporary fix it by setting the analyzer version.

@BeardedGaku This worked for me as well!
I simply added

dependencies:
  ...
  analyzer: 0.39.10

to my pubspec.yaml dependencies and now it works!

I submitted a PR above for a fix, but for now I'm using my own forked repo for the hive_generator, under dev_dependencies:

dev_dependencies:
  flutter_test:
    sdk: flutter

  hive_generator:
    git:
      url: https://github.com/mdrideout/hive
      path: hive_generator
  build_runner: ^1.10.0

Will be switching back to @leisim's package once the fix is enabled.

Since your PR got accepted and your fork is deleted, the fix is:

dev_dependencies:
  ...
  hive_generator:
    git:
      url: https://github.com/hivedb/hive
      path: hive_generator
      ref: 26301e168b586cfe8873dfc2e0d05ebdf015c80d

I used the latest commit as of typing, so it contains 2b49c008f513932267d79b2072d1d25a3811f0b8 just in case.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

yaymalaga picture yaymalaga  路  4Comments

Ferdzzzzzzzz picture Ferdzzzzzzzz  路  3Comments

kaboc picture kaboc  路  3Comments

maxim-saplin picture maxim-saplin  路  3Comments

yannickvg picture yannickvg  路  4Comments