首页 > 解决方案 > 如何使用 2 个流,其中流 2 取决于流 1?

问题描述

我有 2 个流

  Stream<List<GroupModel>> groupStream() {
    final CollectionReference groupCollection = fireStore.collection('groups');

    return groupCollection
        .where('members.${user.id}', isEqualTo: true)
        .snapshots()
        .map((snapshot) => snapshot.documents.map((document) => GroupModel.fromFirestore(document)).toList());
  }

  Stream<List<GroupSubscriptionModel>> groupSubscriptionsStream(List<GroupModel> groups) {
    final chunks = chunkSizeCollection(groups, 10);
    List<Stream<List<GroupSubscriptionModel>>> streams = List<Stream<List<GroupSubscriptionModel>>>();
    chunks.forEach((chunck) => streams.add(fireStore
        .collection('users/${userID}/userGroupSubscriptions')
        .where(FieldPath.documentId, whereIn: chunck)
        .snapshots()
        .map((snapshot) =>
            snapshot.documents.map((document) => GroupSubscriptionModel.fromFirestore(document)).toList())));
    return ZipStream(streams, (value) => value.last);
  }

我想要的是获取所有组,然后从用户那里获取订阅,这些订阅基本上说明用户是否订阅了该组。然后在我的 UI 中,我根据用户是否订阅来切换图标。

我的问题是我groupSubscriptionsStream依赖于组中的 id 来获取相应的文档。因为我不能只流式传输集合中的所有文档。如果可以的话,我没有任何问题。

我正在使用 bloc 和 provider 来为我的小部件提供流控制器。但是我StreamSubscriptionsBloc需要一个List<GroupModel>以便将流添加到它的控制器

class GroupSubscriptionsBloc{

  final StreamController<List<GroupSubscriptionModel>> _controller = StreamController<List<GroupSubscriptionModel>>();
  Stream<List<GroupSubscriptionModel>> get stream => _controller.stream;

  GroupSubscriptionsBloc(DatabaseService database, List<GroupModel> groups)
  {
    _controller.addStream(database.groupSubscriptionsStream(groups));
  }

  void dispose(){
    _controller.close();
  }
}

因此,在小部件中,我有一个静态方法,可为小部件提供两个块

  static Widget create(BuildContext context) {
    final database = Provider.of<DatabaseService>(context);
    return MultiProvider(
      providers: [
        Provider(
          create: (_) => GroupBloc(database),
          dispose: (BuildContext context, GroupBloc bloc) => bloc.dispose(),
        ),
        Provider(
          create: (_) => GroupSubscriptionsBloc(database, null),
          dispose: (BuildContext context, GroupSubscriptionsBloc bloc) => bloc.dispose(),
        ),
      ],
      child: TheGroupOverviewPage(),
    );
  }

但正如您所看到的,我目前将 null 传递给订阅集团,因为我没有组。所以我不能将流添加到他的控制器。

但是我确实想在小部件中使用这两个块TheGroupOverviewPage,因为在不同的小部件中这样做是没有意义的。

所以问题是我如何得到它List<GroupModel>

问题是,如果我能以某种方式组合两个流并将它们映射到 myGroupModel以便我将 isSubscribed 切换为 true,我什至不需要两个流。

class GroupModel
{
  final String _id;
  final String _displayName;
  final Map<String, bool> _members;
  final Map<String, bool> _admins;
  bool isSubscribed = false;

  String get id => _id;
  String get displayName => _displayName;
  Map<String, bool> get members => _members;
  Map<String, bool> get admins => _admins;

  GroupModel._internal(this._id, this._displayName, this._members, this._admins);

  factory GroupModel.fromFirestore(DocumentSnapshot document)
  {
    return GroupModel._internal(
      document.documentID ?? '',
      document['displayName'] ?? '',
      document['members'] != null ? Map.from(document['members']) :  Map<String,bool>(),
      document['admins'] != null ? Map.from(document['admins']) : Map<String,bool>(),
    );
  }
}

我知道您可以组合流,但在这种情况下,第二个流取决于第一个流。那么这甚至是一种选择吗?这将是最有意义的,因为它将输出List<GroupModel>第二个流设置的 isSubscribed 位置。而且我可以将所有相关的组组保存在一个流中。

这就是我目前必须在TheGroupOverview小部件中构建流的方式

    body: StreamBuilder(
      initialData: List<GroupModel>(),
      stream: groupBloc.groupStream,
      builder: (BuildContext context, AsyncSnapshot<List<GroupModel>> groupsSnapshot) {
        return ConditionalWidget(
          condition: groupsSnapshot.connectionState == ConnectionState.active,
          widgetTrue: StreamBuilder(
            initialData: List<GroupSubscriptionModel>(),
            stream: groupSubscriptionsBloc.stream,
            builder: (BuildContext context, AsyncSnapshot<List<GroupSubscriptionModel>> subscriptionSnapshot) {

              groupsSnapshot.data?.forEach((group) => group.isSubscribed =
                  subscriptionSnapshot.data?.any((subscription) => subscription.belongsTo(group)));

              return ConditionalWidget(
                condition: subscriptionSnapshot.connectionState == ConnectionState.active,
                widgetTrue: Builder(
                  builder: (BuildContext context) {
                    return GroupList(
                      groupsSnapshot.data,
                      padding: const EdgeInsets.only(top: 25.0, left: 20.0, right: 20.0),
                      onNotificationIconTap: _onGroupNotificationIconTap,
                    );
                  },
                ),
                widgetFalse: PlatformCircularProgressIndicator(),
              );
            },
          ),
          widgetFalse: PlatformCircularProgressIndicator(),
        );
      },
    ),

编辑 - 在制品

    return groupCollection
        .where('members.${user.id}', isEqualTo: true)
        .snapshots()
        .flatMap((groups) => _groupSubscriptionsStream(groups) , //here should merging happen?));
  }

  Stream<QuerySnapshot> _groupSubscriptionsStream(QuerySnapshot groups) {
    final chunks = chunkSizeCollection(groups.documents.map((group) => group.documentID).toList(), 10);
    List<Stream<QuerySnapshot>> streams = List<Stream<QuerySnapshot>>();
    chunks.forEach((chunck) => streams.add(fireStore
        .collection(APIRoutes.userSubscriptions(user.id))
        .where(FieldPath.documentId, whereIn: chunck)
        .snapshots()));
    return ZipStream(streams, (value) => value.last);

标签: flutterdartgoogle-cloud-firestorestream

解决方案


我最终更改了我的 Firestore 文档,所以我可以进行这样的查询

    return _groupSubscriptionsStream().switchMap((subscriptions) => groupCollection
        .where('members.${user.id}', isEqualTo: true)
        .snapshots()
        .map((snapshot) => snapshot.documents
            .map((document) => GroupModel.fromFirestore(document).checkSubscriptions(subscriptions))));
  }

  Stream<List<GroupSubscriptionModel>> _groupSubscriptionsStream() {
    return fireStore
        .collection(APIRoutes.userSubscriptions(user.id))
        .where('isSubscribed', isEqualTo: true)
        .snapshots()
        .map((snapshot) =>
            snapshot.documents.map((document) => GroupSubscriptionModel.fromFirestore(document)).toList());
  }

所以我现在有一个 isSubscribed 字段,该字段对于查询总是正确的。这确实很好用。


推荐阅读