dart_functional_data icon indicating copy to clipboard operation
dart_functional_data copied to clipboard

Possible to generate `merge` and `apply` methods too?

Open stargazing-dino opened this issue 5 years ago • 1 comments

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()
);

stargazing-dino avatar Nov 07 '19 19:11 stargazing-dino

This seems like a useful feature.

spkersten avatar Nov 27 '19 15:11 spkersten