0

I am trying to create a fetchUserOrders() method in an OrderCubit. To do so, I need the userId which is held in the OrderState of the OrderCubit. Both Cubits are setup in a MultiBlocProvider in main.dart.

How do I make a call like:

Future<void> fetchOrders() async {
    emit(OrderState.loading());
    final uid = context.read<AuthCubit>().state.uid;
    final orders = await OrderRepo().getUserOrders(userId: uid);
    emit(OrderState.loaded(orders));
}

within the OrderCubit?

I don't know of a way to get a context within a Cubit, so the above is not an option thus far. I have looked at using BlocListener, but as I understand it that only emits a new state in response to a change from AuthCubit - which is not refreshing a new value. I appreciate any insight on best practice for reading values from one Cubit to another.

NetizenX
  • 11
  • 4
  • why do you want to read it from the `AuthState` inside the cubit? why don't you send the `uid` as a parameter to the `fetchOrders` method and when you call this method just get the `uid` from the `AuthState` and pass it. – A7MED_SI Feb 11 '23 at 14:45
  • Good point, and I will do that to get it running. It felt ineligant to pass a UID param from the widget into the Cubit, and then to the Repo. Conceptually, I was trying to keep all dependencies cleanly in a Cubit. Additionally, I thought I saw "somewhere" that a cubit can access data from other Cubits, so this is also a question for my own understanding - being pretty new to Cubit. – NetizenX Feb 12 '23 at 06:06
  • In working to implement passing uid as a param, I also ran into the case of OrderCubit.deleteOrder(String orderId); It calles OrderRepo().deleteOrder(documentId: orderId); and then calls fetchOrders(); to get the new list. The uid is not needed for deleting an order directly, but it seems to put me in the spot of passing a param into deleteOrder just to pass to the repo for the "secondary" action of re-fetching orders. – NetizenX Feb 12 '23 at 06:30

0 Answers0