如何读取 StateNotifierProvider.family 而不传值?

How to read StateNotifierProvider.family without passing value?

我已经实现了带有“.family”修饰符的 StateNotifierProvider:

class OrderReviewNotifier extends StateNotifier<OrderReviewState> {
  final OrderReviewRepository repository;

  OrderReviewNotifier(
    this.repository,
    int orderId,
  ) : super(OrderReviewState.initial(orderId));

  Future<void> getOrderItems() async {
    //.....
  }
}

final orderReviewProvider = StateNotifierProvider.autoDispose
    .family<OrderReviewNotifier, OrderReviewState, int>(
  (ref, orderId) {
    return OrderReviewNotifier(
      ref.watch(orderReviewRepositoryProvider),
      orderId,
    );
  },
);

然后在 Consumer 我看它:

        Consumer(
            builder: (context, watch, child) {
              
              final state = watch(orderReviewProvider(order.id));

             //.....
            },
          );

但是当我想看的时候,我也需要通过order.id:

                  onTap: () {
                    context
                        .read(orderReviewProvider(order.id).notifier)
                        .getOrderItems();
                  },

当我想从另一个文件向通知程序发送事件时,我没有order.id。 如何摆脱这种情况? 感谢您的帮助!

我想通了。 我只需要 StateProvider.

final selectedOrderProvider = StateProvider<Order?>((ref) => null);

然后在orderReviewProvider中我可以轻松获取orderId。

final orderReviewProvider =
    StateNotifierProvider.autoDispose<OrderReviewNotifier, OrderReviewState>(
  (ref) {
    return OrderReviewNotifier(
      ref.read,
      orderId: ref.watch(selectedOrderProvider).state!.id,
      repository: ref.watch(orderReviewRepositoryProvider),
    );
  },
);


class OrderReviewNotifier extends StateNotifier<OrderReviewState> {
  OrderReviewNotifier(
    this.read, {
    required int orderId,
    required this.repository,
  }) : super(OrderReviewState.initial(orderId)) {
    getOrderItems();
  }

  final Reader read;

  final OrderReviewRepository repository;

  Future<void> getOrderItems() async {
    state = state.copyWith(
      isLoading: true,
      error: null,
    );
    final result = await repository.getOrderItems(state.orderId);
    final checkedItemIds = await repository.getCheckedItemIds(state.orderId);

    if (!mounted) {
      return;
    }

    result.when(
      data: (data) {
        final isAllItemsChecked = !checkedItemIds.containsValue(false) &&
            checkedItemIds.length >= data.length;

        state = state.copyWith(
          orderItems: data,
          checkedItemIds: checkedItemIds,
          isAllItemsChecked: isAllItemsChecked,
        );
      },
      error: (message) {
        state = state.copyWith(
          error: message,
        );
      },
    );

    state = state.copyWith(
      isLoading: false,
    );
  }
}

文档描述了这口井的工作:link