-
Notifications
You must be signed in to change notification settings - Fork 213
Support spread syntax like Javascript. #891
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
Comments
Dart does have spread operator. Only for lists, tho. |
@mateusfccp Thanks for your tips. I think object spread is more useful. For example I just want to copy the theme data object or some other object, and modify some instance variables. Without this features, maybe I need to write an extra function to do it. |
You can use code-generators to help until Dart has a proper syntax for it. One of them would be: https://github.com/rrousselGit/freezed It has support for a pretty good "copyWith" |
Yes, it would be useful. Currently, a common pattern in Flutter (don't know if it is also for Dart) is to make a function For example, it's used in Flutter's themes/styles. |
Spreads work with all collection types in Dart: lists, maps, and sets. This is a valid Dart program: main() {
const parts = ['shoulders', 'knees'];
const lyrics = ['head', ...parts, 'and', 'toes'];
// ["head", "shoulders", "knees", "and", "toes"]
const obj1 = { 'foo': 'bar', 'x': 42 };
const obj2 = { 'foo2': 'baz', 'y': 13 };
const clonedObj = { ...obj1 };
// Object { foo: "bar", x: 42 }
const mergedObj = { ...obj1, ...obj2 };
// Object { foo: "baz", x: 42, y: 13 }
} All I did was take that JS, put it inside |
@munificent Hi, if i have a identity key in both maps, then this behavior is error in dart, how can I achieve it in dart with a clean and simple way? Update I need to change const to var to make above works. main() {
const obj1 = {'foo': 'bar', 'x': 42};
const obj2 = {'foo': 'baz', 'y': 13};
var clonedObj = {...obj1, 'x': 43};
// var clonedObj = {...obj1}..['x'] = 43;
var mergedObj = {...obj1, ...obj2, 'x': 43};
// var mergedObj = {}..addAll(obj1)..addAll(obj2);
} |
I find it's difficult to do a shallow copy and modify some attributes at the same time like spread syntax in javascript.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax
The following are some js example.
The releated topic is rest parameters.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters
The text was updated successfully, but these errors were encountered: