简体   繁体   English

Flutter Provider - StreamProvider 依赖于另一个 StreamProvider

[英]Flutter Provider - StreamProvider depending on another StreamProvider

TLDR How to use data from one stream provider to call another? TLDR如何使用来自一个 stream 供应商的数据来呼叫另一个供应商?

Background of the Problem问题背景

Hello!你好! I have this Firebase Realtime Database Structure我有这个 Firebase 实时数据库结构

用户数据库结构。

Each user's Group IDs are stored here.每个用户的组 ID 都存储在这里。 They can be accessed to then query the following 'Groups' Json tree.可以访问它们以查询以下“组”Json 树。

群组数据库结构

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:我正在使用提供者 package。要获取用户数据,它就像一个魅力:

  1. I use a Stream Provider我使用 Stream 提供商
  2. I query the database with an onValue function --> Stream.我使用 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我 map 从 Stream 到 class 的数据,并使用 fromMap() 工厂 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.如果我必须访问用户 map 及其嵌套的子级,那将有效。 But as illustrated in the example above, what happens if I have to access the User map first and then the Groups map?但如上例所示,如果我必须先访问用户 map,然后再访问组 map,会发生什么情况? I want to do all of this using stream providers.我想使用 stream 提供商来完成所有这些工作。

you can do it by using the first stream as dependency of the second like below你可以通过使用第一个 stream 作为第二个的依赖来做到这一点,如下所示

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

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

相关问题 刷新/重置 StreamProvider - Flutter Firebase - Refresh/Reset StreamProvider - Flutter Firebase Flutter StreamProvider 不做实时交换 - Flutter StreamProvider does not make realtime exchanes StreamProvider 的初始数据<querysnapshot> .value flutter</querysnapshot> - initial data for StreamProvider<QuerySnapshot>.value flutter Flutter 错误:添加空安全后,StreamProvider 需要 initialData 参数。 流提供者<usermodel>来自 firebase</usermodel> - Flutter Error: StreamProvider requires initialData parameter after null-safety added. StreamProvider<UserModel> from firebase 如何在 Flutter(Dart)的新路线中获取儿童的 StreamProvider 数据 - How to get StreamProvider data in children with new routes in Flutter (Dart) 有没有办法在 StreamProvider 中添加查询参数 - Is there a way to add a query paramater inside a StreamProvider 如何使用 StreamProvider 检查 Firestore 快照 stream 的 ConnectionState? - How check the ConnectionState of Firestore snapshot stream with StreamProvider? StreamProvider - RangeError(索引):无效值:有效值范围为空:0 - StreamProvider - RangeError (index): Invalid value: Valid value range is empty: 0 与提供商 flutter 的身份验证 Firebase - authentification Firebase with Provider flutter 添加电子邮件/密码提供商,即使有另一个提供商使用 email - add Email/Password provider even there's another provider using the email
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM