Is required register a list of Objects in Hive? - flutter

This is my Hive model and I already generate the adapter:
#HiveType(typeId: 7)
class GoalModel {
#HiveField(0)
bool progessNotificaitons;
#HiveField(1)
List<DayModel> daysToWork;
#HiveField(2)
int amountGoal;
#HiveField(3)
DateTime initialDate;
GoalModel({
this.amountGoal,
this.daysToWork,
this.initialDate,
this.progessNotificaitons,
});
}
class DayModel {
String label;
int index;
bool gonnaWork;
DayModel({
this.gonnaWork,
this.index,
this.label,
});
}
The issue is that does not allow me save the daysToWork which is a list of the class DayModel which does not have an adapter. the question is: is required generate an adapter or any special config to save a list of a type of object?
Thanks in advance.

The response is YES, is required generate an register a new adapter even if you just are going to issue the object into a list of another object.

Related

Flutter Hive store color

I need to store colour in my Hive database to retrieve in my eCommerce Application,
it gave me the error below saying that I need to make an adapter, can anyone tell me how to make a colour adapter?
part 'items_model.g.dart';
#HiveType(typeId: 0)
class Item {
#HiveField(0)
final String name;
#HiveField(1)
final double price;
#HiveField(2)
final String? description;
#HiveField(3)
var image;
#HiveField(4)
final String id;
#HiveField(5)
final String shopName;
#HiveField(6)
final List<Category> category;
#HiveField(7)
Color? color;
#HiveField(8)
int? quantity;
Item({
required this.category,
required this.image,
required this.name,
required this.price,
this.description,
required this.id,
required this.shopName,
this.color,
required this.quantity,
});
}
does anyone know how to generate or create Color Adapter? as I don't know-how
E/flutter ( 4621): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: HiveError: Cannot write, unknown type: MaterialColor. Did you forget to register an adapter?
I think the easiest thing to do here would be to store the int value of the color.
Here's an example.
final colorValue = Colors.blue.value; // colorValue is an integer here
So your Hive color could be stored like this
#HiveField(7)
int? colorValue;
Then, in your app when you're creating a color from storage it would look like this.
final item = Item(...however you initialize your Item);
final color = Color(item.colorValue);
Base on previous answers by #Cryptozord and #Loren.A one can simply just write specific adapter for color
import 'package:flutter/cupertino.dart';
import 'package:hive/hive.dart';
class ColorAdapter extends TypeAdapter<Color> {
#override
final typeId = 221;
#override
Color read(BinaryReader reader) => Color(reader.readUint32());
#override
void write(BinaryWriter writer, Color obj) => writer.writeUint32(obj.value);
}
and remember to register your adapter with
Hive.registerAdapter(ColorAdapter());
make sure your typeId is not taken by other models
now you can use
#HiveField(7)
Color? colorValue;
I was confused about this for a whole day and I figured out how to do it. You should instead create your adapter manually like so:
class ItemAdapter extends TypeAdapter<Item> {
#override
final typeId = 0;
#override
ThemeState read(BinaryReader reader) {
return Item(
color: Color(reader.readUint32()),
//the rest of the fields here
);
}
#override
void write(BinaryWriter writer, ThemeState obj) {
writer.writeUint32(obj.color.value);
writer.writeString(obj.name);
//the rest of the fields here
}
}
Color needs to saved and written as a Uint32 value because the Int32 value only has a range of -2147483648 to 2147483647 and an unsigned integer is 0 to 4294967295. The integer values of Color() can go past 2147483647. If Uint32 is not used then your values will not be stored correctly and weird colors will be saved instead.

how to use freezed on a class with custome function that inherits from a non-sealed class in dart?

I have just started learning freezed. I have a GameBase class below:
import 'package:json_annotation/json_annotation.dart';
part 'game_base.g.dart';
#JsonSerializable()
class GameBase {
final int id;
final String facilityName;
final ActivityType activityType;
final Level level;
final DateTime startTime;
final DateTime endTime;
final int participantsNumber;
final String admin;
const GameBase(
{required this.id,
required this.level,
required this.activityType,
required this.admin,
required this.startTime,
required this.facilityName,
required this.endTime,
required this.participantsNumber});
factory GameBase.fromJson(Map<String, dynamic> json) =>
_$GameBaseFromJson(json);
}
Now i have another class called Game, which extends from GameBase. I'm trying to use freezed on this class. I also have a getter in this class. Game class is shown bellow:
part 'game.freezed.dart';
part 'game.g.dart';
#freezed
class Game extends GameBase with _$Game {
Game._();
factory Game({
required List<UserBase> participants,
required String? gameDescription,
required String? activityGroundsName,
required DateTime day,
required double lat,
required double lng,
required int id,
required Level level,
required ActivityType activityType,
required String admin,
required DateTime startTime,
required String facilityName,
required DateTime endTime,
required int participantsNumber,
}) = _Game;
factory Game.fromJson(Map<String, dynamic> json) => _$GameFromJson(json);
get facilityActivityText {
if (activityGroundsName == null) {
return facilityName;
} else {
return facilityName + " - " + activityGroundsName!;
}
}
}
since I have a getter in this class, I must have a private custructor, as mentioned in freezed documentation. However, by doing so I get an error since I extend from GameBase and must call its costructor with its fields.
*Note: I know I can move one field up to GameBase and then have my getter over there without any issue, but since I've just started working with freezed I want to understand it better and find out if there's any way to handle this?
I think your problem's same as this thread. Hope this thread will answer ur questions.
Flutter/Dart: Subclass a freezed data class

Is there a way to get a string representation of a variable in a dart class?

In my Flutter app, I have a class named Task as follows:
class Task {
final String id;
final String title;
final String description;
final DateTime date;
bool isDone;
Task({
required this.id,
required this.title,
this.description = "",
required this.date,
this.isDone = false,
});
}
Now I want to map this class to a database table, means for that I need to map every member of the class with a string to create a table. Like this:
"CREATE TABLE tasks (id TEXT PRIMARY KEY, title TEXT, description TEXT)"
Is there a way in Dart to get a String representation of class members, so to avoid declaring additional static const members just for the names of these members, and also to avoid hardcoding these names every place I need them. Thanks.
You need some reflection, you can get that from the official mirrors library: https://api.dart.dev/stable/2.16.1/dart-mirrors/dart-mirrors-library.html

How to define proper state class in bloc while using freezed plugin

I am trying to understand, how can I use the bloc pattern (specifically state) properly. I am facing this issue for more than a month, but not able to figure out a solution.
Let's consider, I have defined state class following way,
#freezed
abstract class ExampleState with _$ExampleState {
const factory ExampleState.initial() = Initial;
const factory ExampleState.getDataFromServer() = GetDataFromServer; //Thsi will return the ServerData Object.
const factory ExampleState.useServerData() = UseServerData;
const factory ExampleState.changeServerDataAndUpload() = ChangeServerDataAndUpload;
}
Let's consider our Server Data Model is the following way
class ServerData {
final String userId;
final String firstName;
final String lastName;
final String fullAddress;
final String fatherName;
ServerData(
this.userId,
this.firstName,
this.lastName,
this.fullAddress,
this.fatherName,
);
}
In this example, we are able to see, GetDataFromServer, UseServerData, and ChangeServerDataAndUpload state is sharing the same ServerData object. How should I design my state such that, the same DataModel object can be shared between different states?

How to avoid repetition in Flutter while using JsonSerialzable

I have models that I need to map them to JSON and vice versa. Therefore, I've followed flutter's JSON and serialization guide on how to do so.
I found myself writing the same code base for each model, like so:
import 'package:flutter/material.dart';
import 'package:json_annotation/json_annotation.dart';
part 'folder_entity.g.dart';
#JsonSerializable(explicitToJson: true)
class FolderEntity {
final String id;
final String path;
bool isSelected;
FolderEntity({
#required this.id,
#required this.path,
this.isSelected = false,
});
factory FolderEntity.fromMap(Map<String, dynamic> json) => _$FolderEntityFromJson(json);
Map<String, dynamic> toMap() => _$FolderEntityToJson(this);
}
I thought about moving fromMap and toMap to a new abstract class called Entity, but I'm unable to do so for two reasons:
I can't extend a factory, therefore I have write the same pattern for each method.
I'm not sure if there's an option to change prefix from _$FolderEntity to just _$Entity, and if would still be available, will it even work?
For second question you should be change prefix from _$FolderEntity to just _$Entity in folder_entity.g.dart file.