Skip to content

Proposal: Offer a "copy with" generate option #4878

Open
@matthew-carroll

Description

@matthew-carroll

Pressing ctl+enter in Android Studio brings up a menu with auto-generation options. This is often used to generate == and hashCode methods.

In Flutter there is often a need for a copyWith method, which is used to duplicate an existing data structure with some number of alterations. For example:

myTheme.copyWith(
  brightness: Brightness.dark,
  primaryColor: Colors.yellow,
);

A copyWith method might look like:

class MyThing {
  const MyThing({
    this.itemA,
    this.itemB,
    this.itemC,
 });

 final String itemA;
 final int itemB;
 final bool itemC;

 MyThing copyWith({
   String thingA,
   int thingB,
   bool thingC,
}) {
  return MyThing(
    thingA: thingA ?? this.thingA,
    thingB: thingB ?? this.thingB,
    thingC: thingC ?? this.thingC,
  );
}

I think it would be a good idea to add a generation option for a copyWith method, with a generator that would look and operate almost identically to that of == and hashCode.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions