openfoodfacts / openfoodfacts-dart

Open Food Facts API Wrapper
https://pub.dev/packages/openfoodfacts
Apache License 2.0
161 stars 65 forks source link

Add a `copyWith` method to the `Product` object #852

Open g123k opened 9 months ago

g123k commented 9 months ago

Hi everyone,

The Product object has non-final fields, but when we want to use the Product class as immutable, each client must code it. It would be nice to integrate it directly into the SDK.

teolemon commented 9 months ago

We should both be in bed ATM :-)

monsieurtanuki commented 9 months ago

We should both be in bed ATM :-)

image

monsieurtanuki commented 9 months ago

@g123k Currently in Smoothie we do copy Product data, in UpToDateProductProvider:

  // TODO(monsieurtanuki): move code to off-dart Product?
  Product copy(final Product source) => Product.fromJson(
        jsonDecode(jsonEncode(source.toJson())) as Map<String, dynamic>,
      );

Moving this copy code from Smoothie to off-dart would be much less painful than implementing copyWith, and would provide the same feature as all fields are not final and nullable.

g123k commented 9 months ago

I didn't know about this in the app 👍 The only here would be to create a "real" copyWith, not something based on a json, that requires calls to jsonDecode/jsonEncode