r/FlutterDev • u/Mikkelet • Oct 26 '22
Discussion Lack of data classes is really turning me off Flutter
Hi gang, this is not so much a hate thread as it is a "hope to improve". I think flutter is the best cross platform framework out there, and they've gone the extra mile with a lot features, both from a compiler stand point (debugging, hot reload, etc) and a runtime standpoint (unparalled performance).
Working with projects - and especially big ones - has made me realise just how important data classes and auto-serialization is to app and web development. Wanna send a web request? That's json. Want to receive a request? That's JSON. reading and writing local data? JSON. Sending data between apps? JSON. It's all goddamn JSON.
An app with 20 web requests can have up to 40 different JSON classes. Freezed is... a solution to a problem. That's all I will say. A solution to a problem that really shouldn't exist in any app/web framework. And if the JSON has nested json, or god-forbid a list of json objects, it's just hell to work with.
It has gotten to the point where I openly groan whenever we get a API update from the backend team, because it means redefining the freezed classes and deal with the fallout of its generated classes.
For a framework that is so much about streamlining and removing bloat, this particular bloat is so unstreamlined.
So, dear Flutter team, for the love and respect of my time and sanity, PLEASE add data classes.
57
u/chrabeusz Oct 26 '22
Agreed. Thankfully, Dart team is already working on this: https://github.com/dart-lang/language/issues/1482
At the moment, the best solution is for backend team to prepare OpenAPI specification that can be used to generate entire client.
31
u/roman_pro Oct 26 '22
I’d like to emphasize that using the OpenAPI and client generating should be the first and preferred option here. Real-life experience shows that otherwise you end up with unmanageable hand-made REST client where it’s hard to say what features are implemented and which not.
3
4
u/Necator Oct 26 '22
Do you know a tool which already generates client code for flutter by using OpenAPI?
21
u/chrabeusz Oct 26 '22
The official generator supports dart: https://github.com/OpenAPITools/openapi-generator
2
2
u/vipw Oct 26 '22
I use https://pub.dev/packages/swagger_dart_code_generator
The openapi-generator project has 5 dart generators, but I don't know which, if any, of them are any good.
1
u/Only-Split82 Oct 26 '22
Do you know if this packages can handle openapi union types. I mean can it handle the situation that one endpoint may return several different types. This is a big problem with the official openapi generator package and turned me off a bit.
4
u/opsb Oct 26 '22
There's https://pub.dev/packages/graphql_codegen which does the job pretty well for graphql at least. Looking forward to a version that's built on the static metaprogramming though.
20
u/schultek Oct 26 '22
And if the JSON has nested json, or god-forbid a list of json objects, it's just hell to work with.
To ease your pain, check out dart_mappable.
Its a serialization package I made that has excellent support for more complex json, lists and even stuff like polymorphism and generics. It also does some more stuff for data classes like copyWith, toString, etc
3
25
u/esDotDev Oct 26 '22 edited Oct 26 '22
Partly based on the decision to trade runtime-reflection for reduced binary size. I never really got this one, as in the real world, 80%+ of the binary size of any app is the bundled images and fonts. Why kneecap the language itself to get, in the end, a 5% reduction in binary size. I'd gladly trade slightly larger binaries for runtime reflection and easy serialization of any object like C# has.
Also a bit of a bummer that the team is relying on codegen for this intrinsic feature that all other modern languages have. So instead of getting it quickly, it's a multi-yr process that will probably have multiple iterations once landed. Meanwhile the wasted hrs and productivity continue across the entire stack.
With all of the prior art out there, seems like it would be pretty trivial for the right person to add dedicated data class support. :'(
Maybe I'm pessimistic but seems like the code-gen based solution is bound to be more bug-prone or problematic just because of all of the code that is happening under the hood, and the fact it must work with other phases of code-gen. As opposed to a simple focused data class implementation in the compiler itself that would be rock solid and just work.
38
u/fistyit Oct 26 '22
JSON should be a first class citizen in every programming language. I think flutter has conflicting philosophies when it comes to stuff like that, but just decorate a few fields and be done with it is how it should be, kinda like c#
3
u/wstrange Oct 28 '22
Baking in a serialization format into a language is not a great idea.
Ten years ago, XML was all the rage. In another ten years, json will probably be replaced by something else.
1
u/fistyit Oct 28 '22
it's just a module that's not gonna hurt anyone until they need it... but I do understand the point and mostly agree.
3
u/masamunexs Oct 26 '22
I think flutter has conflicting philosophies when it comes to stuff like that, but just decorate a few fields and be done with it is how it should be, kinda like c#
Using the 'freezed' package effectively gives you that. I haven't had to write boiler plate for data classes since.
1
u/fistyit Oct 27 '22
Yeah c# had newtonsoft and they brought in an even simpler version of it to core
5
u/anlumo Oct 26 '22
I don’t know, JSON is horribly inefficient and doesn’t support most data types (like anything binary). It’s fine for high level stuff like web apps, but most languages aren’t used for that.
12
u/qualverse Oct 26 '22
JSON is incredibly fast compared to say XML or YAML. Simdjson can parse >3GB/second. Obviously it's not quite protobuf but i can't think of anything better that's human-readable
3
u/anlumo Oct 26 '22
We switched to CBOR because JSON was too slow for our situation (120MB of JSON data).
Not really human-readable, but it can be translated to JSON for debugging (as long as there's no binary data in there).
1
u/ulfric_tullius Oct 27 '22 edited Oct 27 '22
Look into transit if you want a better way to convey data. https://cognitect.com/blog/2014/7/22/transit I'm not sure if there is a lib for Dart though..
15
u/SuplenC Oct 26 '22
You are right on this one.
One thing I can add is that dart team is actively working on reflection so this might change in the future.
14
u/jonah_williams Oct 26 '22
The Dart team is not working on adding reflection. There is a separate proposal for static metaprogramming, but this does not imply runtime reflection.
2
u/SuplenC Oct 26 '22
Yup saw that issue here.
I saw an issue about reflection too. I might got confused tho with static metaprogramming.
0
Oct 26 '22
[deleted]
1
u/jonah_williams Oct 27 '22
It exists in the VM, but reflection is not actually implemented for AOT compiled dart (profile/release mode) or dart2js (release mode web builds) and dartdevc (debug mode web builds).
Its not clear that it even _is_ implementable, without large regressions in performance. Increased codesize is only one dimension.
6
u/bees4thees Oct 26 '22
Look into using GRPC. It will save both the frontend and backend a lot of trouble and effort.
5
4
u/SuplenC Oct 26 '22
One thing I've found recently that is quite helpful and versatile is this package.
It's model_generator.
Generates models based on a yaml, at least it can regenerate everything when needed.
2
u/vik76 Oct 27 '22
You may want to check out Serverpod. It handles serialization really neatly and also generates your APIs for you.
Small disclaimer, I’m the founder of Serverpod, so I may be slightly biased. 😉
1
u/18pluscomics Jul 07 '24
I think there's a vscode extension that can be used to generate data classes easily. I've used that in the past, and it was a breeze.
1
u/Kingh32 Oct 26 '22
Equatable seems to do a good enough job for data classes. Bit of manual work to add/ remove fields if/ when they change but does the job ok in the grand scheme.
As an alternative to sealed classes (another thing I’ve missed from Kotlin) I’ve taken to having an abstract class and then some classes that implement it, adding whatever fields feel necessary to each of them. Not quite the same but you do kinda, sorta (but not quite!) get to do some of what you’d get with a data class in that you can check for a given type in a switch statement then access the fields on that class (via casting). Handy for immutable state objects, for example.
As for the JSON thing, I just generate the classes whenever there’s an API change. No biggie and I don’t really see the need for something like Freezed as a result.
1
u/ulfric_tullius Oct 27 '22
That is the reason i am excited about ClojureDart. I have yet to find a language, that is better for working with data than Clojure.
1
u/Super-Relief-5827 Feb 19 '24
I coma from xamarin/c# and I was really used to newtosonft, which just maps transparently froim json to class, perfectly.
Has flutter anything like that?
1
u/venir_dev Oct 26 '22
Hold on, why isn't Freezed enough?
2
u/opsb Oct 26 '22 edited Oct 26 '22
Freezed is great, it just needs to be part of the language. At the moment I have a project where every change to a model requires 20secs of code gen. Compared to the lightening fast feedback from the language server it feels more like working with Swift which is what I wanted to get away from.
2
u/venir_dev Oct 26 '22
I see your point, that's slow. Although that process doesn't require fast feedback like hot reload does
0
u/Only-Split82 Oct 26 '22
This is actually not a dart/flutter issue. I mean this is one big challenge of Frontend Development, creating and organizing code to communicate with the backend. There are several tools that may help like openapi generators or client sdk generators. But in the end it is up to the FE team to implement a proper way to communicate with interfaces.
-1
-5
5
u/Direct-Ad-7922 Oct 26 '22
I’m not sure I understand the problem in it’s entirety, although I work with json serializable exclusively.
Are you saying that most modern languages have this built in??