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)
- Do not resolved `typedef`s for records HOT 1
- Mockito creates incomplete mocks for freezed generated classes HOT 2
- [docs]: docs for adding methods which use the data members HOT 1
- Support for Dart 3 Interfaces HOT 3
- Refactor DeepCopy class to use a record
- Invalid generated code when using `Default` instances
- Ability to pinpoint where fromJson throws an error HOT 1
- Support for JsonSerializable Options in @freezed Annotation HOT 3
- Warnings on Generated Code Regarding lowerCamelCase Identifier HOT 2
- Using default values when creating a frezzed sealed class makes using switch type matching difficult. HOT 3
- 2.4.3 - Breaking Changes + Issues HOT 13
- InvalidType is generated when accessing common freezed property HOT 1
- Json encoding produces unexpected object runtimeType HOT 1
- freezed ^2.4.2 is incompatible with `package:test` on latest stable Flutter version HOT 4
- Can't specify a nested list type with @Default() HOT 1
- Cannot name a field `override` because of `@override` annotation HOT 1
- Conflict between @freezed and @JsonSerializable() annotations HOT 2
- `@Default({})` still uses an unmodifiable map with `@unfreezed`. HOT 2
- List<Freezed.Class> toJson not working HOT 1
- Typos in documentation HOT 1
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.