streamData method

  1. @override
Widget streamData({
  1. required String collectionId,
  2. required Widget consumerFunc(
    1. List<Map<String, dynamic>> resultList
    ),
  3. required Map<String, dynamic> where,
  4. required String orderBy,
  5. bool descending = true,
  6. int? limit,
  7. bool hasPage = false,
})
override

Implementation

@override
Widget streamData({
  required String collectionId,
  required Widget Function(List<Map<String, dynamic>> resultList) consumerFunc,
  required Map<String, dynamic> where,
  required String orderBy,
  bool descending = true,
  int? limit, // 페이지 크기
  bool hasPage = false,
}) {
  if (_db == null) {
    return const Text('database is not initialized');
  }
  CollectionReference? collectionRef = _db!.collection(collectionId);

  Query<Object?> query = collectionRef.orderBy(orderBy, descending: descending);
  where.forEach((mid, value) {
    query = query.where(mid, isEqualTo: value.value);
  });

  // 마지막 문서가 있으면, 해당 문서 이후부터 데이터 로드
  if (limit != null) {
    query = query.limit(limit);
  }
  if (startAfter != null && hasPage) {
    query = query.startAfterDocument(startAfter!);
  }

  // Query<Object?> queryRef = collectionRef; // Query 타입으로 초기화

  // 여러 조건이 주어진 경우 where 조건을 추가
  // where.forEach((fieldName, fieldValue) {
  //   queryRef = queryRef.where(fieldName, isEqualTo: fieldValue.value);
  // });

  return StreamBuilder<QuerySnapshot>(
    stream: query.snapshots(),
    builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
      if (snapshot.hasError) {
        return Text('Error: ${snapshot.error}');
      }
      switch (snapshot.connectionState) {
        case ConnectionState.waiting:
          return const Text('Loading...');
        default:
          logger.finest('streamData :  ${snapshot.data!.docs.length} data founded');

          // 마지막 문서 업데이트 (페이징을 위해)
          if (hasPage) {
            startAfter = snapshot.data!.docs.isNotEmpty ? snapshot.data!.docs.last : null;
          }

          return consumerFunc(snapshot.data!.docs.map((doc) {
            return doc.data() as Map<String, dynamic>;
          }).toList());
      }
    },
  );
}