RiverPod Flutter:寻找将FutureProvider与StatenotifierProvider相结合的最佳方法

发布于 2025-01-22 13:49:21 字数 999 浏览 0 评论 0 原文

我正在研究基本的支持票系统。我从Firebase(作为流或未来)那里获得门票。 我想允许一些过滤选项(例如按状态和类别进行排序)。 为此,我考虑使用未来的提供商获取列表和StatenotiferProvider,以根据使用哪种过滤器来更新列表。 这是我到目前为止的代码:

final ticketListStreamProvider =
    RP.FutureProvider((_) => FirestoreService.getTicketList());


class TicketListNotifier extends RP.StateNotifier<List<Ticket>> {
  TicketListNotifier() : super([]);

  void addTicket(Ticket ticket) {
    state = List.from(state)..add(ticket);
  }

  void removeTicket(Ticket ticket) {
    state = List.from(state)..remove(ticket);
  }
}

final ticketsController =
    RP.StateNotifierProvider<TicketListNotifier, List<Ticket>>(
  (ref) => TicketListNotifier(),
);

我有多个问题。首先它行不通。 Statenotifier接受列表,而不是未来&lt; list&gt;。我需要以某种方式将其转换或重写Statenotifier以接受未来。 我试图与一个官方例子之一保持联系。 () 不幸的是,他们没有使用来自firebase之类的外部来源的数据来执行此操作。

解决此问题的最佳方法是什么?

谢谢

I am working on a basic Support Ticket System. I get the Tickets from Firebase (Either as a Stream or Future).
I want to allow some Filtering Options (e.g. sort by Status and Category).
For this, I thought about using A Future Provider to get the List and a StateNotiferProvider to update the List depending on which filter is being used.
This is the code I have so far:

final ticketListStreamProvider =
    RP.FutureProvider((_) => FirestoreService.getTicketList());


class TicketListNotifier extends RP.StateNotifier<List<Ticket>> {
  TicketListNotifier() : super([]);

  void addTicket(Ticket ticket) {
    state = List.from(state)..add(ticket);
  }

  void removeTicket(Ticket ticket) {
    state = List.from(state)..remove(ticket);
  }
}

final ticketsController =
    RP.StateNotifierProvider<TicketListNotifier, List<Ticket>>(
  (ref) => TicketListNotifier(),
);

There are multiple issues I have with that. Firstly it doesn't work.
The StateNotifier accepts a List and not a Future<List>. I need to convert it somehow or rewrite the StateNotifier to accept the Future.
I was trying to stay close to one of the official examples.
(https://github.com/rrousselGit/riverpod/tree/master/examples/todos)
Unfortunately, they don't use data from an outside source like firebase to do it.

What's the best approach to get resolve this issue with which combination of providers?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

罪#恶を代价 2025-01-29 13:49:21
  1. 您可以在 ticketlistNotifier 中获取票务清单,并在您的票务列表中设置其状态。
class TicketListNotifier extends RP.StateNotifier<List<Ticket>> {
  TicketListNotifier() : super([]);

  Future<void> fetchTicketList() async {
    FirestoreService.getTicketList().when(
       //success
       // state = fetched_data_from_firestore
       // error
       // error handle
    )
  }
}

final ticketsController =
    RP.StateNotifierProvider<TicketListNotifier, List<Ticket>>(
  (ref) => TicketListNotifier(),
);

  1. 调用此方法要在窗口小部件的initstate方法中获取 /*
ref.read(ticketsController.notifier).fetchTicketList();

现在 ref.Read(ticketsController); < /code>将返回您的票务

  1. 列表您的添加/删除方法:
ref.read(ticketsController.notifier).addTicket(someTicket);
  1. You can fetch your ticketlist in your TicketListNotifier and set its state with your ticket list.
class TicketListNotifier extends RP.StateNotifier<List<Ticket>> {
  TicketListNotifier() : super([]);

  Future<void> fetchTicketList() async {
    FirestoreService.getTicketList().when(
       //success
       // state = fetched_data_from_firestore
       // error
       // error handle
    )
  }
}

final ticketsController =
    RP.StateNotifierProvider<TicketListNotifier, List<Ticket>>(
  (ref) => TicketListNotifier(),
);

  1. Call this method where you want to fetch it /*maybe in your widget's initState method
ref.read(ticketsController.notifier).fetchTicketList();

Now ref.read(ticketsController); will return your ticket list

  1. Since you have the ticket list in your TicketListNotifier's state you can use your add/remove method like this:
ref.read(ticketsController.notifier).addTicket(someTicket);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文