Comments (14)
Basic support has landed on 0.9.2
Tell me if this is not enough for you 😄
from freezed.
I don't like it.
I do have some ideas on how to solve this issue already anyway. I just need to work on it.
I'll first finish the deep equality comparison and the copyAs, then I'll work on this.
from freezed.
Oh, I've just realized what you did.
That's an interesting case. It's likely because Price
hasn't been generated yet.
from freezed.
No problem 😄
I'll leave this open as it should be able to properly fix this since Freezed
should be able to infer that the type used is a union.
It's not a priority though, as it could get fairly complex with generics because it involves manually parsing the type.
from freezed.
Try to avoid using what's on the right side of the =
of a constructor.
I'll think about a fix, but in the mean time that's your best bet
from freezed.
It doesn't return dynamic
, it's simply not there.
Both PriceExample.price
and PriceExample.withQuantity
define a price
parameter, but they have a different type.
As such you have to use when
/map
/is
to determine if it's a .price
or .withQuantity
, to then be able to access price
.
from freezed.
also does not work for when
or map
because generated class return dynamic.
here is generated class
abstract class PriceWithQuantity implements PriceExample {
const factory PriceWithQuantity(dynamic price, int quantity) =
_$PriceWithQuantity;
dynamic get price; /// this should be [Price] class not dynamic
int get quantity;
PriceWithQuantity copyWith({dynamic price, int quantity});
}
And if I know I am using PriceWithQuantity
class directly it should return instance of Price
with quantity property. for example passing instance of PriceWithQuantity
void addQuantity(PriceWithQuantity quantity){
//code
}
from freezed.
I'm not too sure how to support that.
It may be more logical to write:
@immutable
abstract class Price with _$Price {
const factory Price.price(
int id,
int price,
) = _Price;
}
@immutable
abstract class PriceExample with _$PriceExample {
const factory PriceExample.withQuantity(
Price price,
int quantity,
) = PriceWithQuantity;
}
from freezed.
Or alternatively, you do:
@immutable
abstract class PriceExample with _$PriceExample {
const factory PriceExample(int id, int price) = Price;
const factory PriceExample.withQuantity(
int id,
int price,
int quantity,
) = PriceWithQuantity;
}
from freezed.
Thanks for your suggestion, its just a simple example more properties it will cause a lot of code duplication, but separating class or casting manually for now it just work.
Again Thanks
from freezed.
Remi I think I have a simliar issue but I'm not sure your previous work around is going to work fo rme. I'm trying to model data structure simliar to std Flutter widgets so I have something like this:
import 'package:flutter/foundation.dart';
part 'widget_type.freezed.dart';
@immutable
abstract class WidgetType with _$WidgetType {
const factory WidgetType.page({Body body, Header header}) = Page;
const factory WidgetType.body() = Body;
const factory WidgetType.header({String title}) = Header;
}
It mostly works but in the generated code the Page class uses dynamic instead of the actual types:
abstract class Page implements WidgetType {
const factory Page({dynamic body, dynamic header}) = _$Page;
dynamic get body;
dynamic get header;
Page copyWith({dynamic body, dynamic header});
}
I havent had a chance to look at how the code gen is done, but perhaps it would be possible to do a second pass before writing out the generated src to "patch in" the types for classes that take instances of thier own Union type as parameters?
from freezed.
I havent had a chance to look at how the code gen is done, but perhaps it would be possible to do a second pass before writing out the generated src to "patch in" the types for classes that take instances of thier own Union type as parameters?
Yes. The problem is that this is pretty difficult to do reliably.
The analyzer doesn't expose the source of the unparsed type, so we have to parse it manually. It can get very messy easily with generics and decorators and co
For now the easiest solution is to extract your Body
/Header
as independent classes:
@immutable
abstract class Body with _$Body {
const factory Body() = _Body;
}
@immutable
abstract class Header with _$Header {
const factory Header() = _Header;
}
@immutable
abstract class WidgetType with _$WidgetType {
const factory WidgetType.page({Body body, Header header}) = Page;
}
from freezed.
@rrousselGit it also return dynamic even using separate class, does I'm doing something wrong?
@immutable
abstract class First with _$First{
const factory First.a(String data) = A;
const factory First.b(int data) = B;
}
@immutable
abstract class Second with _$Second{
const factory Second(A a) = SecondConst;
}
from freezed.
@rrousselGit This doesn't work with the current code gen, but perhaps something like this could be made to work just using the existing analyzer approach?
import 'package:flutter/foundation.dart';
part 'widget_type.freezed.dart';
@immutable
abstract class WidgetType with _$WidgetType {
const factory WidgetType() = _WidgetType;
const factory WidgetType.page() = _Page;
}
@immutable
abstract class Page with _$Page implements WidgetType {
const factory Page({Body body, Header header}) = _Page;
}
so its a bit more work for Freezed users but then we are telling Freezed what the specific subclass for each of the Unions factory constructors.
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.