适用于Firebase Firestore的StreamBuilder或BlocBuilder

时间:2019-10-01 15:03:21

标签: firebase flutter stream google-cloud-firestore bloc

我对集团模式以及如何将其与Firebase集成感到困惑。就像我在使用bloc模式时应该使用strambuilder还是仅应使用blocBuilder吗?我还通过代码添加了一些注释,以使您了解使我感到困惑的地方

BlocBuilder(
          //should i put the whole bloc package here or the stream of the bloc only
          bloc: _searchBloc,
          builder: (context, bloc) {
            return StreamBuilder<QuerySnapshot>(
            // is this how am i supposed to get data from firestore with bloc pattern?
                stream: Firestore.instance.collection('Artists').snapshots(),
                builder: (BuildContext context,
                    AsyncSnapshot<QuerySnapshot> snapshot) {
                  if (snapshot.hasError)
                    return new Text('Error: ${snapshot.error}');
                  switch (snapshot.connectionState) {
                    case ConnectionState.waiting:
                      return new Text('Loading...');
                    default:
                      return new ListView(
                        shrinkWrap: true,
                        // here i want the list to be filtered from the value i get from the bloc
                        // however its showing errors because _searchBloc.outSearch is a stream and not a string
                        children: snapshot.data.documents
                            .where((document) => document['name']
                                .toString()
                                .toUpperCase()
                                .contains(_searchBloc.outSearch
                                    .toString()
                                    .toUpperCase()))
                            .map((DocumentSnapshot document) {
                          return SearchItem(
                            artist: Tools.getArtist(document),
                            widthSize: MediaQuery.of(context).size.width,
                            heightSize: Makeover.getVerticalSize(context),
                          );
                        }).toList(),
                      );
                  }
                });
          },
        ),

0 个答案:

没有答案