StreamBuilder: filter stream data and display new results (gets stuck on ConnectionState.waiting)

I am loading data from Firebase and displaying it, this works perfectly fine. Now I want to let the user filter the results on the client-side. This is what doesn’t work.

I am using a StreamController and use the add method to send new user events. Somehow this works fine when getting data from Firebase, but not when I try to switch to the filtered results manually.

Context

Here is the normal code for the initial loading from Firebase:

class UserViewModel {
final UserRepository _userRepo;
      
final StreamController<List<AppUser>> _allUsersStreamcontroller =
      StreamController<List<AppUser>>.broadcast();
List<AppUser> allUsersList = List.empty(growable: true);

// ... other code ...

  Stream<List<AppUser>> getUserStream() {
    return _allUsersStreamcontroller.stream;
  }

Future<bool> loadAllUsersInRadius() async {
    try {
      await _userRepo.getCurrentUserLocation();
    } catch (e) {
      return Future.error(e);
    }

    // Basically load users & auto-filter some results
    if (_userRepo.currentLocation != null) {
      final subcription = _userRepo
          .getAllUsersInRadius(radius: searchRadius)
          .listen((documentList) {
        final filteredUserList =
            _applyAutomaticFilters(documentList, userRelationIDs);

        allUsersList = filteredUserList
            .map((docSnapshot) => _createUserFromSnapshot(docSnapshot))
            .toList();

          _allUsersStreamcontroller.add(allUsersList); // <- Important part, adding users to stream (this works)

      });
      subscriptions.add(subcription);
    }
    return true;
  }

  // ... Other code ...

And StreamBuilder in the UI:

 StreamBuilder<List<AppUser>>(
  // Added random key because otherwise Widget didn't update reliably (suggestion from another SO question)
  key: Key(Random().nextInt(10).toString()),
  stream: userViewModel.getUserStream(), // <- getting stream data
  builder: (context, snapshot) {
    if (snapshot.hasData) {
      if (_swiperEnded || (snapshot.data?.isEmpty ?? false)) {
        // ... display some hint for user about empty list ...
      } else {
        // ... display main UI content ... <- Works fine normally
      }
    } else if (snapshot.connectionState == ConnectionState.waiting) {
      return const Center(child: CircularProgressIndicator()); // <- This is where it gets stuck after I apply manual filters
    } else if (snapshot.hasError || _errorMessage != null) {
      // Display error
    } else {
      // Unimportant
    }
  })

Now here is the part where it doesn’t work. I take the user list and filter the contents based on the filter values. Then I try to add this new list to the same StreamController.

  void applyManualFilters() {
    Iterable<AppUser> filteredUserList = List.from(allUsersList);

    if (isTimeZoneFilterActive)
      filteredUserList = _filterByTimeZoneOffset(filteredUserList);
    if (isAvailabilityFilterActive)
      filteredUserList = _filterByAvailability(filteredUserList);
    if (isAgeFilterActive)
      filteredUserList = _filterByAgeRange(filteredUserList);

    _allUsersStreamcontroller.add(filteredUserList.toList()); // <- setting filtered data here, does NOT work
  }

My Problem

For some reason when I add the filteredUserList to the stream, it gets stuck on ConnectionState.waiting and the emulator starts eating resources like crazy (laptop starts getting very loud). It seems to get stuck in an infinite loop?

What I’ve tried: I tried using two different streams instead and then switching between them in the StreamBuilder based on a bool flag, but that ended the same.


I saw many other questions related to switching streams or similar ideas (1, 2, 3) but all the answers there sit at 0 points or don’t help at all.
I’ve seen one suggestion in this SO question to use BehaviorSubject from RxDart which seems like it’d work, but I’d really prefer not to import a whole state management package just for this one instance.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật