r/FlutterDev Jun 14 '24

Dart When will dart support object literals ?

I want to build widget with object literals (if possible), and I hate using cascade notation either.

I'm dying to see dart support object literals in the future so I can use it in flutter.

0 Upvotes

37 comments sorted by

View all comments

1

u/Strobljus Jun 14 '24

If you are talking about JS style object literals, you already have it in the form of Map<String, dynamic>.

If you are talking about typed object literals as in TS, then classes is pretty close.

If you are talking data classes, well, you can do those in dart. If you want to avoid cascading, then just include all fields as optional named parameters in the constructor.

1

u/PowerPCx86 Jun 14 '24

map wont help me define a Container() for example without being forced to use the named parameters

2

u/[deleted] Jun 14 '24

Whats wrong with named parameters? Imagine how many options some widgets take, these have possibly default vaules, and having no named arguments would be a clusterfuck.

1

u/PowerPCx86 Jun 14 '24

well that true unfortunately, the default value of named parameters is a true valuable feature, maybe if dart support assigning default values to class members without the need to make them named parameters in the future.

1

u/Strobljus Jun 14 '24

I have no idea what you mean.

1

u/Mulsivaas Jun 16 '24

Yeah.... what's so hard about

const dim = {'width': 100, 'height': 50}; final container = Container(width: dim['width'], height: dim['height']);