Questions tagged [freezed]
148 questions
29
votes
2 answers
Freezed and json_serializable: How to use a custom converter
I want to add a custom converter to a freezed class like in this answer.
I tried it with this code:
@freezed
class NewsPost with _$NewsPost {
factory NewsPost({
@JsonKey(name: "date") @TimestampConverter() DateTime? date,
}) = _NewsPost;
…

Valentin Seehausen
- 665
- 1
- 8
- 10
7
votes
2 answers
Flutter, Freezed: Set Class as default value
I have a User class which contains a Purchase class. I want to make it non-nullable, but not required. So this means that I would need to set a default value.
I have no required fields in the sub-class (Purchase), and have provided default…

mrgnhnt96
- 3,562
- 1
- 17
- 36
7
votes
1 answer
How to test a sealed state class in Flutter?
I'm using Freezed to generate sealed data classes in my Flutter app. I need to test it and I don't know how to do so. Any idea? This is the state:
@freezed
abstract class LoginState with _$LoginState {
const factory LoginState({
@required…

Luis Utrera
- 942
- 6
- 15
6
votes
2 answers
Freezed class with enum property throws error upon trying to serialize
I have a freezed class that takes an enum in its constructor, but when trying to perform the jsonEncode method on this class, it fails with the following error:
The following JsonUnsupportedObjectError was thrown while handling a gesture:
Converting…

Luca Cras
- 61
- 1
- 3
6
votes
1 answer
How to inherit from a parent class with Freezed
I'm quite new to Flutter (and even more to the Freezed package...) So I hope that the question is relevant.
So, here is my usecase: a User can be Member of differents groups. A User class shares .
I have issues to deal with this relationship with…

Yako
- 3,405
- 9
- 41
- 71
5
votes
1 answer
Generated class from freezed creates duplicate FromJson method
I have a class which I am trying to use with Freezed, Json Serializable, and Hive. After running dart run build_runner build and generating the necessary classes, my compiler gives me the following error:
: Error: Can't use '_$FooBarFromJson'…

Flik
- 365
- 1
- 7
- 18
5
votes
1 answer
JsonSerializable and JsonKey issue after upgrade on Flutter Freezed model
My app was running ok but after pub upgrade --major-versions I am getting problems on all models.
Example model:
import 'package:app_220/models/Leads/LeadFieldModel.dart';
import 'package:flutter/foundation.dart';
import…

Osmany Becerra Gonzalez
- 139
- 1
- 7
5
votes
2 answers
json_serializable - Add a generic field to a freezed/json_serializable class
How do I make a Freezed object take a generic type? I want to do this:
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:vepo/src/entity_types/option_entity.dart';
part 'vegan_item_tag.freezed.dart';
part…

BeniaminoBaggins
- 11,202
- 41
- 152
- 287
5
votes
1 answer
Freezed class with generic callback
I would like to define a freezed class [https://pub.dev/packages/freezed] with a generic callback.
Freezed class:
import 'package:freezed_annotation/freezed_annotation.dart';
part 'foo.freezed.dart';
@freezed
abstract class Foo with _$Foo {
…

Thierry
- 7,775
- 2
- 15
- 33
5
votes
1 answer
How to use Freezed package with Bloc in flutter?
I want to create a simple bloc with freezed package. This is my bloc:
import 'package:bloc/bloc.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:presentation/presentation_index.dart';
part…

Salma
- 1,211
- 2
- 16
- 33
4
votes
2 answers
Riverpod trigger rebuild when update state with freezed copywith even if nothing changed
I thought Riverpod will only trigger rebuild if the state value is different but turn out it rebuild every time when state is set although the value is the same. Is that true?
The case is as below
@Freezed(genericArgumentFactories: true)
class Model…

Anson Wong
- 1,034
- 8
- 10
4
votes
1 answer
How to stub getter or a method of freezed dataclass in Flutter / Dart
I have a freezed data class with some fields. A getter method returns nested elements of one attribute for easier access.
@freezed
class Airport with _$Airport {
const Airport._();
const factory Airport({
required String identifier
…

tmaihoff
- 2,867
- 1
- 18
- 40
4
votes
1 answer
In Freezed generated classes, how to check if two objects are the same instance?
I'm using Freezed for dart immutable data modelling. This generator is overwriting == operator and the hasCode, which is fine for 99% of cases.
But I have a special case where comparing a long list take time and the List is managed internally.…

angelcervera
- 3,699
- 1
- 40
- 68
4
votes
1 answer
`Unhandled Exception: Invalid argument: Instance of '_$_Category'` While sending data to firestore form a freezed generated class
I created two model classes with freezed. One class is having parameter of other class inside it. The problem started when I try to send data to firestore. This following error appears.
E/flutter ( 6175): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)]…

Usama Karim
- 1,100
- 4
- 15
4
votes
1 answer
flutter : freezed required nor marked with @Default
I am trying to use freezed on my application, So I added
environment:
sdk: ">=2.7.0<3.0.0"
freezed: ^0.14.0
freezed_annotation: ^0.14.0
to my dependencies.
dev_dependencies:
freezed_annotation: ^0.12.0
build_runner: ^2.0.1
…

Cyrus the Great
- 5,145
- 5
- 68
- 149