firebase_facilitator 0.0.5
firebase_facilitator: ^0.0.5 copied to clipboard
A Firebase CRUD package simplifying Firestore operations with optional logging and error tracking, designed for scalable and maintainable Flutter apps.
example/lib/main.dart
import 'dart:developer'; // Used to log messages during the application’s runtime.
import 'package:cloud_firestore/cloud_firestore.dart'; // Firestore package to interact with the Firestore database.
import 'package:firebase_facilitator/mixin/crud_repos.dart'; // Provides CRUD repository mixins.
import 'package:firebase_facilitator/mixin/firestore_read_service.dart'; // Mixin for reading data from Firestore.
import 'package:firebase_facilitator/mixin/firestore_write_service.dart'; // Mixin for writing data to Firestore.
import 'package:firebase_facilitator/mixin/logger_service.dart'; // Mixin for logging services.
void
main() {} // The main entry point of the Dart application. Currently empty but can be used to initiate the app.
/// `ReadWriteReposExample` class demonstrates how to integrate Firestore's
/// read and write operations with the help of mixins provided by the
/// `firebase_facilitator` package.
///
/// This class uses two core mixins:
/// - [FirestoreReadRepository]: For performing read operations from Firestore.
/// - [FirestoreWriteRepository]: For performing write operations to Firestore.
///
/// Additionally, a logger service is used to provide optional logging functionality.
class ReadWriteReposExample
with FirestoreReadRepository, FirestoreWriteRepository {
/// This property returns an instance of the Firestore read service.
///
/// [FirestoreServiceImpl] is a concrete implementation that handles
/// reading data from Firestore.
///
/// The [FirestoreReadService] interface defines methods for fetching
/// data, which the repository uses internally.
@override
FirestoreReadService get firestoreReadService => FirestoreServiceImpl();
/// This property returns an instance of the Firestore write service.
///
/// [FirestoreWriteServiceImpl] is a concrete implementation that handles
/// writing data to Firestore. It enables the repository to perform
/// CRUD operations (Create, Update, Delete).
@override
FirestoreWriteService get firestoreWriteService =>
FirestoreWriteServiceImpl();
/// Logger service for logging operations.
///
/// The logger is enabled here by passing `true` to the [LoggerServiceImpl].
/// It can be used for debugging and tracking purposes within the app.
/// If logging is not needed, this can return `null`.
@override
LoggerService? get loggerService => LoggerServiceImpl(true);
/// The Firestore collection that this repository works with.
///
/// In this example, the collection is named "collection_example".
/// You can modify this getter to work with any other Firestore collection.
@override
String get collection => "collection_example";
/// Custom Firestore operation example that uses Firestore natively
/// to perform queries outside of the mixins provided by the repository.
///
/// This function demonstrates a custom Firestore query:
/// - It queries documents from the specified [collection], where
/// the "field" is not equal to the value "something".
/// - After fetching the result, it logs the size of the result (number of documents).
///
/// This function shows that you can still directly use Firestore alongside
/// the mixin functionalities.
Future<void> customOperations() async {
// Perform a query on the Firestore collection using the native Firestore API.
final result = await FirebaseFirestore.instance
.collection(collection) // Access the collection.
.where("field", isNotEqualTo: "something") // Add a condition.
.get(); // Fetch the documents.
// Log the number of documents found.
log("we found ${result.size} document(s) for you");
}
}