Comments (13)
I will add support for it asap.
I'll go with package:collection
and generate code that uses DeepCollectionEquality
for all Iterables and unconstrained generics
from freezed.
I actually prefer @freezed
.
@immutable
is used in other contexts, too, @freezed
is more googleable.
from freezed.
Personally I'll vote for anything except freezed 😉
from freezed.
Because it requires a peer dependency on package:collection
, I think I'll add an annotation package and switch from @immutable
to @freezed
in the end.
Would that be a problem?
from freezed.
Can't we just use something like https://github.com/passsy/kt.dart ?
from freezed.
I spent some time yesterday looking at all the capabilities of freezed and if I lose any features if I would migrate from built_value to freezed.
Basically everything I need is covered (more or less), but this is the only point where freezed doesn't offer a good solution at the moment if I did not miss something.
So, I want my models to behave like value types, and I want to (de)serialize them.
Dart Collections don't implement == correctly, so I cannot use them.
Even if freezed would special-case dart collections and use DeepCollectionEquality, the lists would still be mutable, which could cause problems.
freezed could potentially either return a copy or a read-only view of dart collections, but this would mean more special casing, I'm not sure if this is the right way.
Now, we could just use immutable collections like kt.dart or built_collection. But these collections are not recognized by json_serializable, and adding support for classes that you don't own is pretty hard.
You'll just get an error like
Error running JsonSerializableGenerator
Could not generate `fromJson` code for `<your list variable>`.
None of the provided `TypeHelper` instances support the defined type.
built_value offers a powerful Serializer architecture, that can be customized relatively easily.
I'm not sure what the best way forward is. If json_serializable would support adding custom types that you don't own, it would be easier.
I'll do some research on how hard it would be to write a custom builder that supports the most used immutable collection types (I think kt.dart and built_collection), but even if we have that, users would probably need to write their own build.yaml file if they want to use it, which is not great either.
from freezed.
something like this might be possible?
@immutable
abstract class Model<T> with _$Model<T> {
const factory Model(
@KtListConverter() KtList<T> Items,
) = _Model<T>;
factory Model.fromJson(Map<String, dynamic> json) =>
_$ModelFromJson(json);
}
class KtListConverter<T> implements JsonConverter<T, Object> {
const KtListConverter();
@override
KtList<T> fromJson(Object json) {
return KtList.from(json.map(map) => Data.fromJson(map)).toList());
}
@override
Object toJson(KtList<T> object) {
return object.map((data) => data.toJson()).toList();
}
}
from freezed.
Can't we just use something like https://github.com/passsy/kt.dart ?
That doesn't work with string literals, which is kind of bad.
for collections need to implement DeepCollectionEquality.
That's a fair request.
But we'd need an annotation package for this to work (which could re-export package:collection).
What about lists of lists though?
from freezed.
something like this might be possible?
package:collection/collection.dart offers DeepCollectionEquality
, which works on everything not just KtList. That's much more reliable.
from freezed.
@smiLLe I don't think that it works on generic types, you would have to write a converter for each type separately.
And it would be nice to have serialization without having to add an annotation on each collection type (although that pain would be tolerable).
from freezed.
I really think is fair enough user your own annotation, it will causes a very easy to repair break changes.
and it will change de dart.meta for a freezed import, will it?
from freezed.
Alright. I'll publish a new version that uses a custom annotation, then I'll work on that.
from freezed.
Haha, it's too late already.
You'll need to set up a filter in your email account 😜
from freezed.
Related Issues (20)
- Confusing legacy tag for "Union types and Sealed classes" HOT 5
- Switch from a model M1 to a model M2 HOT 1
- Arguments of a constant creation must be constant expressions. When adding custom converter with json_serializable HOT 2
- Add support for default value on copyWith
- Add default value for field on copyWith HOT 3
- LinkedHashMap, HashMap is mutable with @freezed HOT 1
- If the path does not start with the lib folder, is it ignored? HOT 1
- Support both analyzer versions HOT 2
- Using macros insted build_runner HOT 1
- freezed and macros HOT 1
- freezed 2.5.3 analyzer 6.5.0 problem HOT 8
- support makeCollectionsUnmodifiable in build.yaml
- Freezed 2.5.3 doesn't work with latest stable Flutter HOT 3
- Annotation to exclude some arguments for toString
- freezed cannot recognize the type which is generated by freezed. HOT 1
- Do not generate a toJson if one is already present on the object
- Bump json_annotation and json_serializable versions
- Using the new `includeFromJson` and `includeToJson` parameters requires json_annotation >= 4.8.0 HOT 1
- Using TweenSequence<FooBar> results in "type _$FooBarImpl might not fully implement..." HOT 1
- Freezed Lint reports unneeded private empty constructor
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from freezed.