Flutter Provider - StreamProvider 依赖于另一个 StreamProvider

发布于 2025-01-18 16:26:16 字数 1113 浏览 2 评论 0原文

tldr 如何使用来自一个流提供商的数据来调用另一个流提供者?

问题的背景

你好!我有这个firebase实时数据库结构

”用户数据库结构。

每个用户的组ID都存储在此处。可以访问它们,然后查询以下“组” JSON树。

这里有一个组的所有数据,多个用户可以访问这些数据。这样的结构是为了避免数据冗余。

问题

我正在使用提供商软件包。为了获取用户数据,它就像魅力一样工作:

  1. 我使用流提供商
  2. ,请查询具有onValue函数的数据库-E>溪流。
  3. 我将数据从该流映射到一个类,并使用FromMap()工厂功能将其转换为该类的结构,
Stream<FrediUser> currentUserDataStream() {
    var userRef = 
    FirebaseDatabase.instance.reference().child('Users').
    child(currentUserID!);
    return userRef.onValue.map((event) => 
    FrediUser.fromMap(event.snapshot.value));
}

如果我必须访问用户映射及其嵌套的孩子,该功能有效。 但是如上所述,如果我必须先访问用户映射,然后组映射会发生什么?我想使用流提供程序来完成所有这些操作。

TLDR
How to use data from one stream provider to call another?

Background of the Problem

Hello! I have this Firebase Realtime Database Structure

Users Database Structure.

Each user's Group IDs are stored here. They can be accessed to then query the following 'Groups' Json tree.

Groups database structure

Here there is all the data for a group, which multiple users can access. The structure is like this to avoid data redundancy.

Problem

I am using the Provider package. To get the user data, it works like a charm:

  1. I use a Stream Provider
  2. I query the database with an onValue function --> Stream.
  3. I map the data from that Stream to a class and transform it to that class's structure using fromMap() factory function
Stream<FrediUser> currentUserDataStream() {
    var userRef = 
    FirebaseDatabase.instance.reference().child('Users').
    child(currentUserID!);
    return userRef.onValue.map((event) => 
    FrediUser.fromMap(event.snapshot.value));
}

That works if I have to access the User map and its nested children. But as illustrated in the example above, what happens if I have to access the User map first and then the Groups map? I want to do all of this using stream providers.

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

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

发布评论

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

评论(1

離殇 2025-01-25 16:26:16

您可以通过使用第一个流作为第二个流的依赖项来完成此操作,如下所示

StreamProvider<FrediUser>(
      create: (context) => firstStream(),
      initialData: 'initialData',
      builder: (context, child) {
        return StreamProvider(
          create: (context) => secondStream(
              context.watch<FrediUser>(),
          ),
          initialData: 'initialData',
          child: child,
        );
      },
    )

you can do it by using the first stream as dependency of the second like below

StreamProvider<FrediUser>(
      create: (context) => firstStream(),
      initialData: 'initialData',
      builder: (context, child) {
        return StreamProvider(
          create: (context) => secondStream(
              context.watch<FrediUser>(),
          ),
          initialData: 'initialData',
          child: child,
        );
      },
    )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文