Search code examples
flutterdartgoogle-cloud-firestorestream-builder

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


I'm using StreamBuilder to stream some data from Firestore.

It's working good and the data is appearing but there's an error in the console.

That's the code that I think it may contains the error:

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,
                              ),
                            ),
                        ],
                      ),
                    ),
                  ),
                ),

and the error:

enter image description here

Any help?!

Note: I know that 'for' here is not supported in the old versions of Dart.


Solution

  • Before create your column from your snapshot, be sure that your snapshot has data, 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());
          },
        ),
      ),
    )