"The method 'where' was called on null." 将 streambuilder 与 firestore flutter 结合使用时

"The method 'where' was called on null." when using streambuilder with firestore flutter

我正在使用 StreamBuilder 从 Firestore 流式传输一些数据。

运行良好,数据正在显示,但控制台出现错误。

这是我认为可能包含错误的代码:

Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 16),
                  child: Consumer<AppState>(
                    builder: (context, appState, _) =>
                        StreamBuilder<List<Dentist>>(
                      stream: database.dentistsStream(),
                      builder: (context, snapshot) => Column(
                        children: <Widget>[
                          for (final dentist in snapshot.data.where((e) => e
                              .categoryIds
                              .contains(appState.selectedCategoryId)))
                            GestureDetector(
                              onTap: () {},
                              child: DentistItem(
                                dentist: dentist,
                              ),
                            ),
                        ],
                      ),
                    ),
                  ),
                ),

和错误:

enter image description here

有什么帮助吗?!

注意:我知道这里的'for'在老版本的Dart中是不支持的

在从快照创建列之前,请确保您的快照包含数据,if (snapshot.hasData)

Padding(
  padding: const EdgeInsets.symmetric(horizontal: 16),
  child: Consumer<AppState>(
    builder: (context, appState, _) => StreamBuilder<List<Dentist>>(
      stream: database.dentistsStream(),
      builder: (context, snapshot) {
        if (snapshot.hasData)
          return Column(
            children: <Widget>[
              for (final dentist in snapshot.data.where((e) =>
                  e.categoryIds.contains(appState.selectedCategoryId)))
                GestureDetector(
                  onTap: () {},
                  child: DentistItem(
                    dentist: dentist,
                  ),
                ),
            ],
          );
        return Center(child: CircularProgressIndicator());
      },
    ),
  ),
)