Flutter/Riverpod创建复杂对象的副本,并在深处某处更改值

huangapple go评论58阅读模式
英文:

Flutter/Riverpod creating copy of complex object with changing value somewhere deep

问题

I will provide the translation of the non-code parts of your text:

"fellow coders. I am learning Riverpod state management rn. In the documentation, author of the Riverpod suggest to use immutability as much as possible. So i guess to manage state of some complex object I have to create copy of state instance. I have some sandbox project to test functions of Riverpod. There is complex Object called Menu(for restaurant) (see code bellow). And I have no clue how to emit new state with some change somewhere deep in it.

using freezed and fast_immutable_collections (for IList), is it right?

There is class Menu with subclass StaticMenu (permanent offer of meal) in here is a list of MealGroup (e.g., pork, pasta, vegan,...) and each group has a unique id, and also contains a list of class Meal also with a unique id. Now, I want to create methods that emit new state but with added/updated/deleted Meal from down below, but I guess I have a lack of knowledge of the syntax, and I have no clue how to write it down.

thank you for any help :)"

If you need any further assistance with specific parts of the code or have any questions, please feel free to ask.

英文:

fellow coders. I am learning Riverpod state management rn. In the documentation, author of the Riverpod suggest to use immutability as much as possible. So i guess to manage state of some complex object I have to create copy of state instance. I have some snadbox project to test functions of Riverpod. There is complex Object called Menu(for restaurant) (see code bellow). And I have no clue how to emit new state with some change somewhere deep in it.

using freezed and fast_immutable_collections (for IList), is it right?

@freezed
class Menu with _$Menu {
  const factory Menu({
    required String? currency,
    required WeeklyMenu? weeklyMenu,
    required StaticMenu? staticMenu,
  }) = _Menu;

  factory Menu.fromJson(Map<String, Object?> json) =>
      _$MenuFromJson(json);
}

@freezed
class WeeklyMenu with _$WeeklyMenu {
  const factory WeeklyMenu({
    required IList<Day>? dayList,
  }) = _WeeklyMenu;

  factory WeeklyMenu.fromJson(Map<String, Object?> json) =>
      _$WeeklyMenuFromJson(json);
}

@freezed
class StaticMenu with _$StaticMenu {
  const factory StaticMenu({
    required IList<MealGroup>? mealGroupList,
    required IList<DrinkGroup>? drinkGroupList,
  }) = _StaticMenu;

  factory StaticMenu.fromJson(Map<String, Object?> json) =>
      _$StaticMenuFromJson(json);
}

@freezed
class Day with _$Day {
  const factory Day({
    required String? id,
    required IList<Meal>? mealList,
    required bool? isChecked,
  }) = _Day;

  factory Day.fromJson(Map<String, Object?> json) => _$DayFromJson(json);
}


@freezed
class MealGroup with _$MealGroup {
  const factory MealGroup({
    required String? id,
    required String? description,
    required String? note,
    required IList<Meal>? mealList,
    required bool? isChecked,
  }) = _MealGroup;

  factory MealGroup.fromJson(Map<String, Object?> json) =>
      _$MealGroupFromJson(json);
}

@freezed
class DrinkGroup with _$DrinkGroup {
  const factory DrinkGroup({
    required String? id,
    required String? description,
    required String? note,
    required IList<Drink>? drinkList,
    required bool? isChecked,
  }) = _DrinkGroup;

  factory DrinkGroup.fromJson(Map<String, Object?> json) =>
      _$DrinkGroupFromJson(json);
}

@freezed
class Meal with _$Meal {
  const factory Meal({
    required String? id,
    required String? name,
    required String? price,
  }) = _Meal;

  factory Meal.fromJson(Map<String, Object?> json) => _$MealFromJson(json);
}


@freezed
class Drink with _$Drink {
  const factory Drink({
    required String? id,
    required String? name,
    required String? price,
  }) = _Drink;

  factory Drink.fromJson(Map<String, Object?> json) => _$DrinkFromJson(json);
}

There is class Menu with subclass StaticMenu (permanent offer of meal) in here is list of MealGroup (4ex. pork, pasta, vegan,...) and each group has unique id, and also contains list of class Meal aslo with unique id. Now a want to create methods, that emits new state but with added/updated/deleted Meal from down bellow, but i guess I have lack of knowledge of the syntax and I have no clue how to write it down.


@riverpod
class MenuNotifier extends _$MenuNotifier {
  @override
  Menu build() {
    return const Menu(
      currency: 'Kč',
      staticMenu: null,
      weeklyMenu: null,
    );
  }

  void initializeMenu() async {
    final menu = await ServerHelper.initializeLocalJson();
    state = menu;
  }

  Future<void> updateOnServer() async {
    ServerHelper.sendMenuToServer(state);
  }

  void updateMealInMealGroup({
    required String mealGroupUid,
    required String mealUid,
    required Meal meal,
  }) {
    //state = state.copyWith(staticMenu: state.staticMenu?.mealGroupList);
  }

  void addMealToMealGroup({
    required String mealGroupUid,
    required String mealUid,
    required Meal meal,
  }) {  }

  void deleteMealinMealGroup({
    required String mealGroupUid,
    required String mealUid,
  }) {  }
}

thank you for any help Flutter/Riverpod创建复杂对象的副本,并在深处某处更改值

答案1

得分: 1

如果你参考Freezed: Going further: Deep copy中的内容,那么通常不应该有问题。一切都是极其不可变的 Flutter/Riverpod创建复杂对象的副本,并在深处某处更改值

英文:

If you are guided by the item Freezed: Going further: Deep copy, then in general there should be no problem. Everything is extremely immutable Flutter/Riverpod创建复杂对象的副本,并在深处某处更改值

huangapple
  • 本文由 发表于 2023年3月9日 16:08:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75681900.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定