-1

I want to use Hive for local database for storage. This is my model class

    import 'package:flutter/material.dart';
import 'package:hive/hive.dart';
part 'addForm.g.dart';

@HiveType(typeId: 0)
class AddForm extends HiveObject{

  @HiveField(0)
  String title;

  @HiveField(1)
  String description;

  @HiveField(2)
  int fieldNumber;

  @HiveField(3)
  String question;

  @HiveField(4)
  Widget fieldType;

  @HiveField(5)
  bool required;

}

I use use this command: "flutter packages pub run build_runner build" to generate .g.dart file but it's not generating.

I tried everything but nothing is working please help!

leftjoin
  • 36,950
  • 8
  • 57
  • 116
Hadi Khan
  • 90
  • 1
  • 9
  • In your case probably a error is thrown because `Widget` is no `HiveType`. In your terminal you should see the reason. – Mäddin Sep 16 '21 at 10:49

5 Answers5

1

This answer is for who made the same mistake.

Because I faced the same problem. In my case, my part '..'; was the same name as class name just like your code above. This won't work, you should name the part as the name of your file_name.dart not in your class ClassName.

for example.

if your file name is add_form.dart then your part is part 'add_form.g.dart';

also, hive doesn't support widget.

Noryn Basaya
  • 664
  • 1
  • 5
  • 21
0

HiveFields may only have types that are directly supported by Hive or are HiveTypes. Widget is neither, which is why you cannot save a widget unless you have written a corresponding adapter yourself.

Your terminal should also show this as an error.

Mäddin
  • 1,070
  • 6
  • 11
0

Try this , without (typeId:0).. @HiveType() class MyNote extends HiveObject {}

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
JB Jason
  • 291
  • 2
  • 8
0

An easy solution is to first clean the build cache and get the dependencies.

To clean the cache:

flutter clean

To install the build cache:

flutter pub get

Then run the command:

flutter pub run build_runner build
Pranav
  • 1
  • 3
-1

I was facing the same issue, i re-run the same command and the file generated :)

ASHAR MEHMOOD
  • 21
  • 1
  • 1
  • 5
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 15 '22 at 19:37