I want to ask a question that is about a hydrated bloc with Freezed.
Firstly, I know that the we use freezed package to gain a time. We do not create all the necessary codes like creating copyWith method etc. So, If I check the generated file, I can see the toJson, fromJson methods. The problem is, I do not know should I create these methods in the state file like below code/image.
I create my state management solution with freezed, and I do not know how can I use freezed with a hydrated bloc.
To test and understand the logic, I created an app which is a counter app basically.
So, in the application folder, I have CounterCubit
and CounterState
.
Here my the CounterState
file:
part of 'counter_cubit.dart';
@freezed
class CounterState with _$CounterState {
const factory CounterState({
required int number,
}) = _CounterState;
const CounterState._();
factory CounterState.empty() => const CounterState(number: 0);
factory CounterState.fromJson(Map<String, dynamic> json) => _$CounterStateFromJson(json);
@override
Map<String, dynamic> toJson() => _$CounterStateToJson(this);
}
You can see the below image gives an error, something is wrong.
In the cubit file I put also .g
file to generate. Here I confused because I use both freezed
package and json_serializable
package.
I removed json_seril. package, and then wrote my own functions. I also create a sample for this, If you want to use hydrated bloc with freezed, you can find the usage of the hydrated bloc with freezed in here: https://github.com/alperefesahin/hydrated_bloc_freezed_sample