Skip to content

Possible to generate merge and apply methods too? #13

@stargazing-dino

Description

@stargazing-dino

Hi! First off, I love this package. It has solved so many problems for me.

My current use case is when a user signs in, I have their configuration for the app saved in Firebase. If it's their first time signing in, I have to populate it with reasonable empty fields. This is what that currently looks like:

// Configuration extends $Configuration and also uses json_serializable
//
// Inside document.get().then((snapshot) =>
final empty = Configuration.empty();

if (snapshot.exists) {
  final configuration = Configuration.fromJson(snapshot.data);

  await configurationDocument.setData(
    Configuration(
      primaryColor: configuration.primaryColor ?? empty.primaryColor,
      // A lot more fields . . .
    ).toJson(),
  );
} else {
  await configurationDocument.setData(empty.toJson());
}

With merge I think could just say something like:

await configurationDocument.setData(
  configuration.merge(empty).toJson()
);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions