繁体   English   中英

Flutterriverpod 递增 state 与通知器

[英]Flutter riverpod incrementing state with notifier

在以前版本的Riverpod中,我可以通过copyWith轻松地增加 state 中的一些数据,但在riverpod: ^1.0.3版本的这个库中我不能这样做。 例如:

/* click event to increment */
ref.read(orderStructureProvider.notifier).increment(breadId: id);

order通知器:

final orderStructureProvider = StateNotifierProvider<OrderNotifier, OrderStructure>((ref) => OrderNotifier());

class OrderNotifier extends StateNotifier<OrderStructure> {
  OrderNotifier() : super(_initial);

  static const OrderStructure _initial = OrderStructure(
    address: Address(
      address: 'address',
      province: {},
      city: {},
      neighbourhood: {},
    ),
    breads: [],
    baker: [],
    paymentType: 1,
  );

  void resetOrder() => state = _initial;

  int breadsCount() => state.breads.length;

  void updateAddress(
    String _address,
    Map<String, dynamic> _province,
    Map<String, dynamic> _city,
    Map<String, dynamic> _neighbourhood,
  ) {
    state = OrderStructure(
      address: Address(
        address: _address,
        province: _province,
        city: _city,
        neighbourhood: _neighbourhood,
      ),
      breads: [...state.breads],
      baker: [],
      paymentType: state.paymentType,
    );
  }

  BreadStructure? getService(int breadId) {
    final index = state.breads.indexWhere((bread) => bread.id == breadId);
    return index >= 0 ? state.breads[index] : null;
  }

  void create(BreadStructure _bread) {
    final newState = OrderStructure(
      address: state.address,
      breads: [...state.breads, _bread],
      baker: [...state.baker],
      paymentType: state.paymentType,
    );
    //newState.breads=[...state.breads,_bread];
    state = newState;
  }

  bool breadExists(int breadId) => state.breads.indexWhere((bread) => bread.id == breadId) == -1;

  void increment({required int breadId}) {
    final newState = OrderStructure(
      address: state.address,
      breads: [...state.breads],
      baker: [...state.baker],
      paymentType: state.paymentType,
    );

    debugPrint(state.breads.toString());
    final int index = newState.breads.indexWhere((element) => element.id == breadId);
    newState.breads[index].copyWith(count: 44);
    //debugPrint(newState.breads.toString());
    state = newState;
  }

  void decrement(int breadId) {
    final newState = OrderStructure(
      address: state.address,
      breads: [...state.breads],
      baker: [...state.baker],
      paymentType: state.paymentType,
    );

    final index = state.breads.indexWhere((element) => element.id == breadId);
    BreadStructure bread = newState.breads[index];
    if (index >= 0) {
      if (bread.count <= 1) {
        newState.breads.removeAt(index);
      } else {
        bread = bread.copyWith(count: bread.count - 1);
      }
    }

    state = newState;
  }
}

@freezed
abstract class OrderStructure with _$OrderStructure {
  const factory OrderStructure({
    required Address address,
    required List<BreadStructure> breads,
    required List<Map<String, dynamic>> baker,
    required int paymentType,
  }) = _OrderStructure;

  factory OrderStructure.fromJson(Map<String, dynamic> json) => _$OrderStructureFromJson(json);
}

@freezed
abstract class Address with _$Address {
  const factory Address({
    required String address,
    required Map<String, dynamic> province,
    required Map<String, dynamic> city,
    required Map<String, dynamic> neighbourhood,
  }) = _Address;

  factory Address.fromJson(Map<String, dynamic> json) => _$AddressFromJson(json);
}

@freezed
abstract class BreadStructure with _$BreadStructure {
  const factory BreadStructure({
    required int id,
    required String name,
    required int count,
  }) = _BreadStructure;

  factory BreadStructure.fromJson(Map<String, dynamic> json) => _$BreadStructureFromJson(json);
}

output:

0
1
1
1
1
1
...

问题是

newState.breads[index].copyWith(count: 44);

copyWith()正在返回带有修改参数的新实例,而不是编辑当前实例。

解决方案

void increment({required int breadId}) {
    final int index = state.breads.indexWhere((bread) => bread.id == breadId);
    final bread = state.breads[index];

    final updatedBread = bread.copyWith(count: bread.count + 1);
    
    /* save to state */
    state = state.copyWith(
      breads: List.from(state.breads)..[index] = updatedBread,
    );
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM