TurboFirestoreApi<T> class
A powerful, type-safe wrapper around Cloud Firestore operations.
The TurboFirestoreApi provides a high-level interface for interacting with Firestore, offering automatic type conversion, validation, and enhanced error handling.
Type parameter T
represents the model type this API instance will work with.
Features:
- Automatic type conversion between Firestore documents and Dart objects
- Built-in validation through TurboWriteable
- Automatic timestamp management for createdAt/updatedAt fields
- Local ID management for easier document tracking
- Sensitive data handling
- Comprehensive logging
- Collection group support
- Batch operation capabilities
Example usage with a custom model:
class User {
User({required this.name, this.age});
final String name;
final int? age;
factory User.fromJson(Map<String, dynamic> json) => User(
name: json['name'] as String,
age: json['age'] as int?,
);
Map<String, dynamic> toJson() => {
'name': name,
'age': age,
};
}
final api = TurboFirestoreApi<User>(
firebaseFirestore: FirebaseFirestore.instance,
collectionPath: () => 'users',
fromJson: User.fromJson,
toJson: (user) => user.toJson(),
);
// Create a new user
final user = User(name: 'John', age: 30);
await api.set(data: user);
// Query users
final adults = await api.query(
where: (ref) => ref.where('age', isGreaterThanOrEqualTo: 18),
);
Constructors
-
TurboFirestoreApi.new({required FirebaseFirestore firebaseFirestore, required String collectionPath(), Map<
String, dynamic> toJson(T value)?, T fromJson(Map<String, dynamic> json)?, T fromJsonError(Map<String, dynamic> json)?, bool tryAddLocalId = false, TurboFirestoreLogger? logger, String createdAtFieldName = 'createdAt', String updatedAtFieldName = 'updatedAt', String idFieldName = 'id', String documentReferenceFieldName = 'documentReference', bool isCollectionGroup = false, bool tryAddLocalDocumentReference = false, GetOptions? getOptions, bool hideSensitiveData = true}) - Creates a new instance of TurboFirestoreApi.
Properties
-
collection
→ CollectionReference<
Object?> -
The current collection
no setter
-
doc
→ DocumentReference<
Object?> -
A new document
no setter
- genId → String
-
Generates a new document ID without creating the document
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- writeBatch → WriteBatch
-
Helper method to fetch a
WriteBatch
from_firebaseFirestore
..no setter
Methods
-
createDoc(
{required TurboWriteable writeable, String? id, WriteBatch? writeBatch, TurboTimestampType createTimeStampType = TurboTimestampType.createdAtAndUpdatedAt, TurboTimestampType updateTimeStampType = TurboTimestampType.updatedAt, bool merge = false, List< FieldPath> ? mergeFields, String? collectionPathOverride, Transaction? transaction}) → Future<TurboResponse< DocumentReference< >Object?> > -
Available on TurboFirestoreApi, provided by the TurboFirestoreCreateApi extension
Creates or writes a document to Firestore. -
createDocInBatch(
{required TurboWriteable writeable, String? id, WriteBatch? writeBatch, TurboTimestampType createTimeStampType = TurboTimestampType.createdAtAndUpdatedAt, TurboTimestampType updateTimeStampType = TurboTimestampType.updatedAt, bool merge = false, List< FieldPath> ? mergeFields, String? collectionPathOverride}) → Future<TurboResponse< WriteBatchWithReference< >Map< >String, dynamic> > -
Available on TurboFirestoreApi, provided by the TurboFirestoreCreateApi extension
Creates or writes documents using a batch operation. -
deleteDoc(
{required String id, WriteBatch? writeBatch, String? collectionPathOverride, Transaction? transaction}) → Future< TurboResponse> -
Available on TurboFirestoreApi<
Deletes a document from FirestoreT> , provided by the TurboFirestoreDeleteApi extension -
deleteDocInBatch(
{required String id, WriteBatch? writeBatch, String? collectionPathOverride}) → Future< TurboResponse< WriteBatchWithReference< >Map< >String, dynamic> > -
Available on TurboFirestoreApi<
Deletes documents using a batch operationT> , provided by the TurboFirestoreDeleteApi extension -
docExists(
{required String id, String? collectionPathOverride}) → Future< bool> -
Used to determined if a document exists based on given
id
. -
getById(
{required String id, String? collectionPathOverride}) → Future< TurboResponse< Map< >String, dynamic> > -
Available on TurboFirestoreApi<
Retrieves a document by its unique identifierT> , provided by the TurboFirestoreGetApi extension -
getByIdWithConverter(
{required String id, String? collectionPathOverride}) → Future< TurboResponse< T> > -
Available on TurboFirestoreApi<
Retrieves and converts a document by its unique identifierT> , provided by the TurboFirestoreGetApi extension -
getDocRefById(
{required String id, String? collectionPathOverride}) → DocumentReference< Map< String, dynamic> > -
Available on TurboFirestoreApi<
Gets a document reference by ID for raw data accessT> , provided by the TurboFirestoreGetApi extension -
getDocRefByIdWithConverter(
{required String id, String? collectionPathOverride}) → DocumentReference< T> -
Available on TurboFirestoreApi<
Gets a document reference with type conversionT> , provided by the TurboFirestoreGetApi extension -
getDocSnapshotById(
{required String id, String? collectionPathOverride}) → Future< DocumentSnapshot< Map< >String, dynamic> > -
Available on TurboFirestoreApi<
Gets a document snapshot for raw data accessT> , provided by the TurboFirestoreGetApi extension -
getDocSnapshotByIdWithConverter(
{required String id, String? collectionPathOverride}) → Future< DocumentSnapshot< T> > -
Available on TurboFirestoreApi<
Gets a document snapshot with type conversionT> , provided by the TurboFirestoreGetApi extension -
listAll(
) → Future< TurboResponse< List< >Map< >String, dynamic> > -
Available on TurboFirestoreApi<
Lists all documents in the collectionT> , provided by the TurboFirestoreListApi extension -
listAllWithConverter(
) → Future< TurboResponse< List< >T> > -
Available on TurboFirestoreApi<
Lists and converts all documents in the collectionT> , provided by the TurboFirestoreListApi extension -
listByQuery(
{required CollectionReferenceDef< Map< collectionReferenceQuery, required String whereDescription}) → Future<String, dynamic> >TurboResponse< List< >Map< >String, dynamic> > -
Available on TurboFirestoreApi<
Lists documents matching a custom queryT> , provided by the TurboFirestoreListApi extension -
listByQueryWithConverter(
{required CollectionReferenceDef< T> collectionReferenceQuery, required String whereDescription}) → Future<TurboResponse< List< >T> > -
Available on TurboFirestoreApi<
Lists and converts documents matching a custom queryT> , provided by the TurboFirestoreListApi extension -
listBySearchTerm(
{required String searchTerm, required String searchField, required TurboSearchTermType searchTermType, bool doSearchNumberEquivalent = false, int? limit}) → Future< TurboResponse< List< >Map< >String, dynamic> > -
Available on TurboFirestoreApi<
Searches for documents matching a search termT> , provided by the TurboFirestoreSearchApi extension -
listBySearchTermWithConverter(
{required String searchTerm, required String searchField, required TurboSearchTermType searchTermType, bool doSearchNumberEquivalent = false, int? limit}) → Future< TurboResponse< List< >T> > -
Available on TurboFirestoreApi<
Searches for documents with type conversionT> , provided by the TurboFirestoreSearchApi extension -
listCollectionReference(
) → Query< Map< String, dynamic> > -
Available on TurboFirestoreApi<
Gets a collection reference for raw data accessT> , provided by the TurboFirestoreListApi extension -
listCollectionReferenceWithConverter(
) → Query< T> -
Available on TurboFirestoreApi<
Gets a collection reference with type conversionT> , provided by the TurboFirestoreListApi extension -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
runTransaction<
E> (TransactionHandler< E> transactionHandler, {Duration timeout = const Duration(seconds: 30), int maxAttempts = 5}) → Future<E> -
Helper method to run a
Transaction
from_firebaseFirestore
.. -
streamAll(
) → Stream< QuerySnapshot< Map< >String, dynamic> > -
Available on TurboFirestoreApi<
Streams all documents from a collection with exception handlingT> , provided by the TurboFirestoreStreamApi extension -
streamAllWithConverter(
) → Stream< List< T> > -
Available on TurboFirestoreApi<
Streams and converts all documents from a collection with error handlingT> , provided by the TurboFirestoreStreamApi extension -
streamByDocId(
{required String id, String? collectionPathOverride}) → Stream< DocumentSnapshot< Map< >String, dynamic> > -
Available on TurboFirestoreApi<
Streams a single documentT> , provided by the TurboFirestoreStreamApi extension -
streamByQuery(
{required CollectionReferenceDef< Map< ? collectionReferenceQuery, required String whereDescription}) → Stream<String, dynamic> >List< Map< >String, dynamic> > -
Available on TurboFirestoreApi<
Streams documents matching a queryT> , provided by the TurboFirestoreStreamApi extension -
streamByQueryWithConverter(
{CollectionReferenceDef< T> ? collectionReferenceQuery, required String whereDescription}) → Stream<List< T> > -
Available on TurboFirestoreApi<
Streams and converts documents matching a queryT> , provided by the TurboFirestoreStreamApi extension -
streamDocByIdWithConverter(
{required String id, String? collectionPathOverride}) → Stream< T?> -
Available on TurboFirestoreApi<
Streams and converts a single documentT> , provided by the TurboFirestoreStreamApi extension -
toString(
) → String -
A string representation of this object.
inherited
-
turboVars<
V extends TurboApiVars> () → V -
Returns a new instance of
V
with basic variables filled in. -
updateDoc(
{required TurboWriteable writeable, required String id, WriteBatch? writeBatch, TurboTimestampType timestampType = TurboTimestampType.updatedAt, String? collectionPathOverride, Transaction? transaction}) → Future< TurboResponse< DocumentReference< >Object?> > -
Available on TurboFirestoreApi<
Updates an existing document in FirestoreT> , provided by the TurboFirestoreUpdateApi extension -
updateDocInBatch(
{required TurboWriteable writeable, required String id, WriteBatch? writeBatch, TurboTimestampType timestampType = TurboTimestampType.updatedAt, String? collectionPathOverride}) → Future< TurboResponse< WriteBatchWithReference< >Map< >String, dynamic> > -
Available on TurboFirestoreApi<
Updates documents using a batch operationT> , provided by the TurboFirestoreUpdateApi extension
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited