FirestorePlus<T> class

A generic class that provides type-safe Firestore operations.

This class encapsulates all Firestore operations and ensures type safety through Dart generics. It requires a constructor function to create instances of the generic type from Firestore data.

Basic Usage

// Create a FirestorePlus instance for User model
final userCollection = FirestorePlus<User>.instance(
  tConstructor: User.withMap,
  path: 'users',
);

// Add a new user
final user = User(uid: '', name: 'John Doe', email: 'john@example.com');
final userId = await userCollection.add(object: user);

// Get all users as a stream
StreamBuilder<List<User>>(
  stream: userCollection.streams,
  builder: (context, snapshot) {
    if (snapshot.hasData) {
      final users = snapshot.data!;
      return ListView.builder(
        itemCount: users.length,
        itemBuilder: (context, index) => Text(users[index].name),
      );
    }
    return CircularProgressIndicator();
  },
);

Type Safety

The generic type T must implement InterfacePlus to ensure:

  • Proper serialization/deserialization
  • Consistent document ID handling
  • Type-safe operations across the entire API

Constructor Function

The tConstructor parameter is a function that creates instances of type T from Firestore data. This function is called whenever FirestorePlus needs to deserialize documents from Firestore.

// Example constructor function
static User withMap(Map<String, dynamic> map) => User(
  uid: map['uid'],
  name: map['name'],
  email: map['email'],
);

Performance Considerations

  • Streams provide real-time updates but consume resources
  • Use limit parameter to control query size
  • Consider using futures for one-time data fetching
  • Sub-collections can impact performance with large datasets

Error Handling

All methods can throw Firestore exceptions. Always wrap operations in try-catch:

try {
  final userId = await userCollection.add(object: user);
  print('User added with ID: $userId');
} catch (e) {
  print('Error adding user: $e');
}

Constructors

FirestorePlus.instance({String? app, String? uid = "none", int limit = 10, required T tConstructor(Map<String, dynamic>), required String path, String? subPath})
Creates a new FirestorePlus instance for type-safe Firestore operations.

Properties

app String?
Firebase app name for multi-app configurations.
final
delete Future<String>
Deletes a document from the collection.
no setter
future Future<T>
Future that resolves to a single document.
no setter
futures Future<List<T>>
Future that resolves to all documents in the collection.
no setter
futureSubCollection Future<Map<String, Stream<List<T>>>>
Future that resolves to sub-collections for each document.
no setter
hashCode int
The hash code for this object.
no setterinherited
limit int
Maximum number of documents to fetch in collection queries.
final
path String
Firestore collection path.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
stream Stream<T>
Stream of a single document with real-time updates.
no setter
streams Stream<List<T>>
Stream of all documents in the collection with real-time updates.
no setter
streamSubCollection Stream<Map<String, Stream<List<T>>>>
Stream of sub-collections for each document in the main collection.
no setter
subPath String?
Sub-collection path for nested operations.
final
tConstructor → T Function(Map<String, dynamic>)
Constructor function for creating instances of type T from Firestore data.
final
uid String?
Document identifier for single document operations.
final

Methods

add({required InterfacePlus object}) Future<String>
Adds a new document to the collection.
addByUid({required InterfacePlus object}) Future
Adds a document with a specific UID.
futureWithSearch({required String searchField, required String search}) Future<List<T>>
Future that resolves to documents filtered by a single field.
futureWithSearch2({required String searchField1, required String search1, required String searchField2, required String search2}) Future<List<T>>
Future that resolves to documents filtered by two fields.
listDocumentIds() Future<List<String>>
Returns a list of document IDs in the current collection.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
streamWithSearch({required String searchField, required String search}) Stream<List<T>>
Stream of documents filtered by a single field.
streamWithSearch2({required String searchField1, required String search1, required String searchField2, required String search2}) Stream<List<T>>
Stream of documents filtered by two fields.
subCollection<U>({required String subPath, required String? itemUid, required U tConstructor(Map<String, dynamic>)}) FirestorePlus<U>
Creates a FirestorePlus instance for a sub-collection.
toString() String
A string representation of this object.
inherited
update({required InterfacePlus object}) Future<String>
Updates an existing document in the collection.

Operators

operator ==(Object other) bool
The equality operator.
inherited