firestore_wrapper library
A type-safe wrapper for Cloud Firestore operations in Flutter.
This library provides a strongly-typed interface for working with Firestore, making it easier to handle data with proper types and reducing runtime errors.
Key features:
- Type-safe collections and documents
- Simplified batch operations
- Transaction helpers
- Query builder with pagination support
- Real-time updates with streams
Basic usage:
// Define a model class
class User {
final String id;
final String name;
final int points;
User({
required this.id,
required this.name,
required this.points,
});
static User fromJson(Map<String, dynamic> json, String id) {
return User(
id: id,
name: json['name'] as String,
points: json['points'] as int? ?? 0,
);
}
}
// Create a typed collection
final usersCollection = Fb.collection<User>(
'users',
User.fromJson,
);
// CRUD operations
final userDoc = usersCollection.doc('123');
await userDoc.set({
'name': 'John Doe',
'points': 100,
});
// Query with type safety
final topUsers = await usersCollection
.query()
.orderBy('points', descending: true)
.limit(10)
.get();
// Real-time updates
userDoc.stream().listen((user) {
if (user != null) {
print('User updated: ${user.name}');
}
});
// Batch operations
final batch = Fb.batch();
batch.set('users/123', {'status': 'active'});
batch.update('users/456', {'points': 200});
batch.delete('users/789');
await batch.commit();
// Transactions
await Fb.runTransaction((transaction) async {
final userData = await transaction.get('users/123');
if (userData != null) {
transaction.update('users/123', {
'points': userData['points'] + 100,
});
}
});
Classes
- Fb
- The main entry point for the Firestore wrapper functionality.
- FbBatch
- A wrapper class for Firestore batch operations.
- FbTransaction
- A wrapper for Firestore transactions that provides a simplified interface for performing atomic operations.
-
FirebaseCollection<
T> - A type-safe wrapper for Firestore collections.
-
FirebaseDocument<
T> - A type-safe wrapper for Firestore documents.
-
FirebaseQuery<
T> - A type-safe wrapper for Firestore queries.
-
QueryResult<
T> - A container for paginated query results from Firestore.