How can I listen to document changes inside referenced documents in firestore?

Is it possible to listen to changes inside referenced documents within an array ‘events’ in my collection ‘calendar’. Within this array I save references to another collection where I store my calendar item information that I then read out per day in my calendar?

With my current code it only listens to changes in the calendar documents. But not in the referenced documents.

calendar document structure:

array : events
   0: assignments/reference(docid)
   1: assignments/reference(docid)

assignments document structure :

String date : dd/MM/yyyy
String eventType : Public opening
String time : HH:mm-HH:mm
String responsible : username
  void listenToDocumentChanges(DateTime selectedDay) {
    final formattedDate = DateFormat('yyyy-MM-dd').format(selectedDay);

    FirebaseFirestore.instance
        .collection('calendar')
        .doc(formattedDate)
        .snapshots()
        .listen((calendarDocSnapshot) async {
      try {
        state = state.copyWith(isLoading: true, hasError: false);

        if (calendarDocSnapshot.exists) {
          final data = calendarDocSnapshot.data() ?? {};
          final events = (data['events'] as List?)?.cast<dynamic>() ?? [];

          final resolvedEvents = await resolveEvents(events);

          // Update the cache with events for the selected day
          state = state.copyWith(
            eventsCache: {
              ...state.eventsCache,
              formattedDate: resolvedEvents,
            },
            isLoading: false,
          );

          print(
              'Updated cache with ${resolvedEvents.length} events for       $formattedDate');
        } else {
          state = state.copyWith(
            eventsCache: {
              ...state.eventsCache,
              formattedDate: [],
            },
            isLoading: false,
          );

          print('No document exists for date: $formattedDate');
        }
      } catch (e) {
        logError('Error in listenToDocumentChanges', e);

        state = state.copyWith(
          eventsCache: {
            ...state.eventsCache,
            formattedDate: [],
          },
          isLoading: false,
          hasError: true,
        );
      }
    }, onError: (error) {
      logError('Snapshot listener error', error);

      state = state.copyWith(
        eventsCache: {
          ...state.eventsCache,
          formattedDate: [],
        },
        isLoading: false,
        hasError: true,
      );
    });
  }

    Future<List<Map<String, dynamic>>> resolveEvents(List<dynamic> events) async {
    try {
      final List<Map<String, dynamic>> resolvedEvents = [];
      final List<DocumentReference> references = [];

      for (var event in events) {
        if (event is DocumentReference) {
          references.add(event);
        } else if (event is Map<String, dynamic>) {
          resolvedEvents.add(event);
        }
      }

      if (references.isNotEmpty) {
        final snapshots = await Future.wait(references.map((ref) => ref.get()));
        for (var snapshot in snapshots) {
          if (snapshot.exists) {
            final data = snapshot.data() as Map<String, dynamic>?;
            if (data != null) {
              resolvedEvents.add({
                ...data,
                'reference': snapshot.reference.path,
              });
            }
          }
        }
      }

      return resolvedEvents;
    } catch (e) {
      logError('Error resolving events', e);
      return [];
    }
  }

    void logError(String message, dynamic error) {
    print('$message: $error');
  }

Recognized by Google Cloud Collective

New contributor

Ser is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

If you want to also listen for changes in linked/references documents, you’ll need to set up separate listeners for that. Firestore does not have a built-in feature for that.

So:

  1. Read the events as you currently do.
  2. Set up separate listeners for the event documents.
  3. Track your listeners, so you can detach then when they’re no longer needed.
Recognized by Google Cloud Collective

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