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;
}
With hive, before you can read/compose data, a box should be opened. Boxes can be opened with await Hive. Openbox('name') can get an instance of an opened box with Hive. Box ('name'), where 'name' is the name of the case (saying the DB name). You can call Hive.
First we will see how we can use Hive for XML. In this, we are going to load XML data into Hive tables, and we will fetch the values stored inside the XML tags. Step 1) Creation of Table "xmlsample_guru" with str column with string data type. Step 2) Using XPath () method we will be able to fetch the data stored inside XML tags.
Using Hive as data store we can able to load JSON data into Hive tables by creating schemas. In this, we are going to load JSON data into Hive tables, and we will fetch the values stored in JSON schema. Step 1) In this step, we are going to create JSON table name "json_guru".
Data Extraction in Hive means the creation of tables in Hive and loading structured and semi structured data as well as querying data based on the requirements. For batch processing, we are going to write custom defined scripts using a custom map and reduce scripts using a scripting language.
From Spark 2.0, you can easily read data from Hive data warehouse and also write/append new data to Hive tables. Append data to the existing Hive table via both INSERT statement and append write mode. Python is used as programming language. The syntax for Scala will be very similar.
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 ) .
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With