Problem while flutter build runner register generator for using the hive database in flutter

Viewed 3784

I am getting below error while generating .g.dart file by build runner command. Could you please help to figure out what's wrong here?

[INFO] Creating build script snapshot......
[INFO] Creating build script snapshot... completed, took 6.8s

[SEVERE] Failed to snapshot build script .dart_tool/build/entrypoint/build.dart.
This is likely caused by a misconfigured builder definition.
[SEVERE] ../../../vaibhav/flutter/flutter/.pub-cache/hosted/pub.dartlang.org/hive-1.4.1+1/lib/src/registry/type_registry_impl.dart:14:7: Error: The non-abstract class 'TypeRegistryImpl' is missing implementations for these members: - TypeRegistry.noTry to either - provide an implementation, - inherit an implementation from a superclass or mixin, - mark the class as abstract, or - provide a 'noSuchMethod' implementation.class TypeRegistryImpl implements TypeRegistry {      ^^^^^^^^^^^^^^^^../../../vaibhav/flutter/flutter/.pub-cache/hosted/pub.dartlang.org/hive-1.4.1+1/lib/src/registry/type_registry.dart:7:7: Context: 'TypeRegistry.no' is defined here.  int no;      ^^../../../vaibhav/flutter/flutter/.pub-cache/hosted/pub.dartlang.org/hive-1.4.1+1/lib/src/hive_impl.dart:21:7: Error: The non-abstract class 'HiveImpl' is missing implementations for these members: - TypeRegistry.noTry to either - provide an implementation, - inherit an implementation from a superclass or mixin, - mark the class as abstract, or - provide a 'noSuchMethod' implementation.class HiveImpl extends TypeRegistryImpl implements HiveInterface {      ^^^^^^^^../../../vaibhav/flutter/flutter/.pub-cache/hosted/pub.dartlang.org/hive-1.4.1+1/lib/src/registry/type_registry.dart:7:7: Context: 'TypeRegistry.no' is defined here.  int no;      ^^../../../vaibhav/flutter/flutter/.pub-cache/hosted/pub.dartlang.org/hive-1.4.1+1/lib/src/registry/type_registry_impl.dart:35:8: Error: The method 'TypeRegistryImpl.registerAdapter' has fewer positional arguments than those of overridden method 'TypeRegistry.registerAdapter'.  void registerAdapter<T>(TypeAdapter<T> adapter, {bool internal = false}) {       ^../../../vaibhav/flutter/flutter/.pub-cache/hosted/pub.dartlang.org/hive-1.4.1+1/lib/src/registry/type_registry.dart:9:8: Context: This is the overridden method ('registerAdapter').  void registerAdapter<T>(TypeAdapter<T> adapter,int no);       ^../../../vaibhav/flutter/flutter/.pub-cache/hosted/pub.dartlang.org/hive-1.4.1+1/lib/src/hive_impl.dart:21:7: Error: The implementation of 'registerAdapter' in the non-abstract class 'HiveImpl' does not conform to its interface.class HiveImpl extends TypeRegistryImpl implements HiveInterface {      ^^^^^^^^../../../vaibhav/flutter/flutter/.pub-cache/hosted/pub.dartlang.org/hive-1.4.1+1/lib/src/registry/type_registry_impl.dart:35:8: Context: The method 'TypeRegistryImpl.registerAdapter' has fewer positional arguments than those of overridden method 'TypeRegistry.registerAdapter'.  void registerAdapter<T>(TypeAdapter<T> adapter, {bool internal = false}) {       ^../../../vaibhav/flutter/flutter/.pub-cache/hosted/pub.dartlang.org/hive-1.4.1+1/lib/src/registry/type_registry.dart:9:8: Context: This is the overridden method ('registerAdapter').  void registerAdapter<T>(TypeAdapter<T> adapter,int no);       ^../../../vaibhav/flutter/flutter/.pub-cache/hosted/pub.dartlang.org/hive-1.4.1+1/lib/src/hive_impl.dart:21:7: Error: Class 'HiveImpl' inherits multiple members named 'registerAdapter' with incompatible signatures.Try adding a declaration of 'registerAdapter' to 'HiveImpl'.class HiveImpl extends TypeRegistryImpl implements HiveInterface {      ^^^^^^^^
pub finished with exit code 78

This is my pubspec.yaml

description: A new Flutter application.

version: 1.0.0+1

environment:
  sdk: ">=2.2.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter
  hive: ^1.4.1+1
  hive_flutter: ^0.3.0+2
  path_provider: ^1.6.5

  cupertino_icons: ^0.1.2

dev_dependencies:
  flutter_test:
    sdk: flutter
  hive_generator: ^0.7.0+2
  build_runner: ^1.8.0


flutter:

  uses-material-design: true```
3 Answers

worked for me after upgrading build_runner to

  hive_generator: ^1.1.1
  build_runner: ^2.0.5

There are some issues with the Hive Generator. You can overcome this by doing the following:

  1. Ensure your model class is correctly defined. That is, you have non conflicting @HiveType and @HiveField annotations on the class and properties respectively. As an example..

     import 'dart:core';
     import 'package:hive/hive.dart';
     part 'user_profile.g.dart';
    
     @HiveType(typeId: 2)
     class UserProfile {
    
        @HiveField(0)
        final String firstname;
        @HiveField(1)
        final String lastname;
        @HiveField(2)
        final String email;
        @HiveField(3)
        final String mobile;
        @HiveField(4)
        final DateTime lastRefreshedAt;
    
        UserProfile(this.firstname, this.lastname, this.email, this.mobile, this.lastRefreshedAt);
    
     }
    
  2. Run your build runner command in the cmd or terminal

  3. This will generate your part file ... you will now need to edit this file. (do note that every time you run build runner this edit will be overwritten so you will have to do it again.

      // GENERATED CODE - DO NOT MODIFY BY HAND
    
      part of 'user_profile.dart';
    
      // **************************************************************************
      // TypeAdapterGenerator
      // **************************************************************************
    
      class UserProfileAdapter extends TypeAdapter<UserProfile> {
        // Add the following two lines
        @override
        final typeId = 2;
        .
        .
        . 
      }
    

(Not ideal but until they fix the generator this is a workaround. In our project we have submitted this to this source control and developers must discard changes to this file.)

Use part 'classname_model.g.dart'; then use command flutter packages pub run build_runner build in terminal to generate your model

Related