我有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来获取相应的文档。因为我不能只将所有文档流式传输到集合中。如果可以的话,我没有任何问题。
我正在使用带有locator的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>
?
事实是,如果我能以某种方式组合两个流并将它们映射到我的GroupModel
,以便将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);
我最终更改了我的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字段,对于查询始终为true。这确实很有效。