djalmafreestyler
djalmafreestyler

Reputation: 1909

How to save a List<Object> and retrieve using Hive?

I have a Wallpaper App and it uses Firestore to store the wallpapers.

I want to use Hive to store a list of wallpapers from cloud firestore but how to save the List of Wallpapers and retrieve it later?

When I try to save the list I get this error:

E/flutter ( 9995): [ERROR:flutter/shell/common/shell.cc(199)] Dart Error: Unhandled exception: E/flutter ( 9995): HiveError: Cannot write, unknown type: Wallpaper. Did you forget to register an adapter?

Code:

class Wallpaper extends HiveObject {


  String date;
  String url;

  Wallpaper();

}

static Future<void> addWallpapers({@required String boxName, @required List<Wallpaper> wallpapers}) async {

    var box = await Hive.openBox(boxName);
    box.put(boxName, wallpapers);

    print("WALLPAPER ADICIONADO NO HIVE!");

  }

  static Future<List<Wallpaper>> getWallpapers({@required String boxName}) async {

    var box = await Hive.openBox(boxName);

    List<Wallpaper> wallpapers = box.get("latest");

    return wallpapers;

  }

Upvotes: 17

Views: 26923

Answers (3)

Hadiuzzaman
Hadiuzzaman

Reputation: 454

You need to perform the following steps:

Step 1: Add those dependencies on pubspec.yaml file

dependencies:
  hive: ^2.2.3
  hive_flutter: ^1.1.0

dev_dependencies:
  hive_generator: ^1.1.3
  build_runner: ^[latest version]

Step 2: Make the class like this


part 'wallpaper.g.dart';

@HiveType(typeId: 0, adapterName: "WallpaperAdapter")
class Wallpaper{

  @HiveField(0)
  final String date;

  @HiveField(1)
  final String url;

  Wallpaper({required this.date, required this.url});

}

Step 3: Run the command on terminal

1. flutter packages pub run build_runner build

                      or

2. flutter packages pub run build_runner watch --delete-conflicting-outputs

Step 4: Now register the adapter on main.dart

  void main() async {

  WidgetsFlutterBinding.ensureInitialized();
  await Hive.initFlutter();
  Hive.registerAdapter(WallpaperAdapter());
  await Hive.openBox('myBox');

  runApp(MyApp());
}

Step 5: Now restart your application and let's enjoy 😊

Upvotes: 2

derZorn
derZorn

Reputation: 254

I solved the problem by including an actual ID on HiveType. Like this:

  @HiveType(typeId: 0)
  class SoundSingle {

    @HiveField(0)
    final String name;

    @HiveField(1)
    final String fileName;

    @HiveField(2)
    int volume;

    SoundSingle(this.name,this.fileName, this.volume);
}

More HiveType models need to increment the number. So each value is unique ( and I guess sequential, but I did not test on that ) .

Upvotes: 4

Rodrigo Bastos
Rodrigo Bastos

Reputation: 2448

You have to anotate your object with @HiveType(). And have to register your object Hive.registerAdapter(WallpaperAdapter(), 0);.

And yet, do you have part 'wallpaper.g.dart'; to generate the needed code?

EDITED: First of all import the dependencies on your pubspec:

dependencies:
  hive: ^[version]
  hive_flutter: ^[version]

dev_dependencies:
  hive_generator: ^[version]
  build_runner: ^[version]

The Hive.registerAdapter(MyObjectAdapter(), 0); you should put inside your main.dart function. Right before runApp

Your HiveObject should have annotations like that:

@HiveType()
class Person extends HiveObject {
  @HiveField(0);
  String name;

  @HiveField(1);
  int age;
}

Put this command near your imports part 'person.g.dart'; and run the code generation on your terminal. flutter packages pub run build_runner build.

Hive function with code generation, so this command will generate the file you need

Upvotes: 8

Related Questions