New post on /r/flutterdev subreddit:
<b>Abstract API class</b>
What are the best practices to refactor this class?​<pre>class ClinicRepository { final FirebaseFirestore _firebaseFirestore; final CategoryRepository _categoryRepository; final Geoflutterfire _geoflutterfire; GeoFirePoint center; double radius = 5.0; bool hasMoreTopRated = true; bool hasMoreCategory = true; bool hasMoreReviews = true; CollectionReference _clinicCollection; CollectionReference _doctorCollection; CollectionReference _reviewsCollection; DocumentSnapshot _lastTopRatedDocument; DocumentSnapshot _lastCategoryDocument; DocumentSnapshot _lastReviewDocument; ClinicRepository({ CenterPoint centerPoint, FirebaseFirestore firebaseFirestore, Geoflutterfire geoflutterfire, LocationServices locationServices, CategoryRepository categoryRepository, }) : _firebaseFirestore = firebaseFirestore ?? FirebaseFirestore.instance, _geoflutterfire = geoflutterfire ?? Geoflutterfire(), _categoryRepository = categoryRepository ?? CategoryRepository() { _clinicCollection = _firebaseFirestore.collection("clinics"); _doctorCollection = _firebaseFirestore.collection("doctors"); _reviewsCollection = _firebaseFirestore.collection("reviews"); center = GeoFirePoint(centerPoint.latitude, centerPoint.longitude); } Future<List<Clinic>> getNearbyClinics({int limit = 10}) async { List<Clinic> clinics = []; List<DistanceDocSnapshot> snapshot = await _geoflutterfire .collection(collectionRef: _clinicCollection) .within(center: center, radius: radius, field: 'position', limit: limit) .first; for (var clinic in snapshot) { final categoryId =
clinic.documentSnapshot.data()['category']; final category = await _categoryRepository.getCategoryById(categoryId); clinics.add(Clinic.fromJson(
clinic.documentSnapshot.id, clinic.distance,
clinic.documentSnapshot.data()) ..category =
category.name); } return clinics; } Future<List<Clinic>> getTopRatedClinics(int limit, {bool isRefresh = false}) async { if (isRefresh) { _lastTopRatedDocument = null; hasMoreTopRated = true; } if (!hasMoreTopRated) return []; QuerySnapshot snapshot; if (_lastTopRatedDocument != null) { snapshot = await _clinicCollection .orderBy('rate', descending: true) .startAtDocument(_lastTopRatedDocument) .limit(limit) .get(); } else { snapshot = await _clinicCollection .orderBy('rate', descending: true) .limit(limit) .get(); } if (snapshot.docs.length < limit) { hasMoreTopRated = false; } _lastTopRatedDocument = snapshot.docs.last; return
snapshot.docs.map((clinic) { final clinicData = Clinic.fromJson(
clinic.id, getDistance(
clinic.data()['position']['geopoint']),
clinic.data()); bool isClinicOpen = isOpen(clinicData.openTime, clinicData.closeTime); return clinicData..isOpen = isClinicOpen; }).toList(); } Future<List<Clinic>> getClinicsByCategory(String categoryID, int limit, {bool isRefresh = false}) async { assert(categoryID != null); if (isRefresh) { _lastCategoryDocument = null; hasMoreCategory = true; } if (!hasMoreCategory) return []; QuerySnapshot snapshot; if (_lastCategoryDocument != null) { snapshot = await _clinicCollection .where('category', isEqualTo: categoryID) .startAfterDocument(_lastCategoryDocument) .limit(limit) .get(); } else { snapshot = await _clinicCollection .where('category', isEqualTo: categoryID) .limit(limit) .get(); } if (snapshot.docs.length < limit) { hasMoreCategory = false; } _lastCategoryDocument = snapshot.docs.last; return
snapshot.docs.map((clinic) { final clinicData = Clinic.fromJson(
clinic.id, getDistance(
clinic.data()['position']['geopoint']),
clinic.data()); bool isClinicOpen = isOpen(clinicData.openTime, clinicData.closeTime); return clinicData..isOpen = isClinicOpen; }).toList(); } Future<List<Review>> getClinicReviews(String clinicId, {int limit = 3, isRefresh = false}) async { assert(clinicId != null); if (isRefresh) { _lastReviewDocument = null; hasMoreReviews = true; } if (!hasMoreReviews) return []; QuerySnapshot snapshot; if (_lastReviewDocument != null) { snapshot = await _reviewsCollection…