-
Notifications
You must be signed in to change notification settings - Fork 205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Object spread like TypeScript has #1781
Comments
Just to be clear, does the Dart equivalent to what you're asking look something like this: class A {
String prop1;
int prop2;
bool prop3;
A({required this.prop1, required this.prop2, required this.prop3});
}
class B {
String prop1;
int prop2;
B({required this.prop1, required this.prop2});
}
void main() {
A a = A(prop1: "Hello World", prop2: 42, prop3: true);
B b1 = B(...a);
// equal to:
B b2 = B(prop1: a.prop1, prop2: a.prop2);
} Couple of notes:
class A {
String prop1;
int prop2;
bool prop3;
A({required this.prop1, required this.prop2, required this.prop3});
}
class B extends A {
bool prop3;
B({required String prop1, required int prop2, required this.prop3}) :
super(prop1: prop1, prop2: prop2);
} You may be interested in a proposal at #493 that reduces the above to: class B extends A {
bool prop3;
B({required super.prop1, required super.prop2, required this.prop3});
} This doesn't make it easier for someone who has an |
This sounds remarkably close to reflection. Reflection is easy in JavaScript because objects are really just maps. That's not the case in Dart. Iterating over the names and values of "properties" in Dart is more complicated. You cannot create an object simply from its property values, you need to go through a constructor. You cannot access properties given just their name. And you cannot iterate the properties of an object to begin with because that's reflection, and it will prevent tree-shaking properties that aren't used in the program (which our AoT compilers try to do). |
Is there any chance to be able to spread objects like we can in TypeScript?
Consider following interface in TS (code simplified)
and React component A which accepts these Props
where ComponentB defines its props like
Why to have similar feature in Dart?
In Flutter especially, if we compose custom widgets I've finding myself many times passing around same arguments to nested widgets. Maintainance of such widgets becomes very troublesome.
Another significant added value is for mapping between objects like from DTO to Domain Entity where Domain Entity typically have nearly same properties + added functionality.
If such issue already exists, feel free to close it. I couldn't find it.
The text was updated successfully, but these errors were encountered: